Swift Basic Grammar
This article, courtesy of the Swift Chinese development team, provides an in-depth explanation of the Swift language’s syntax for iOS development. We will cover the basics of constants and variables, type annotations, constant and variable naming, and more.
Constants and Variables
In Swift, constants and variables are declared using the let
and var
keywords, respectively. Constants, once assigned a value, cannot be changed, while variables can be altered. The following example demonstrates how to declare a constant and a variable to record the user’s login attempts:
let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0
Here, maximumNumberOfLoginAttempts
is declared as a constant with a value of 10, while currentLoginAttempt
is declared as a variable initialized to 0.
You can declare multiple constants in a row or more variables, separated by commas:
var x = 0.0, y = 0.0, z = 0.0
Type Annotations
When declaring a constant or variable, you can add type annotations to illustrate the type of value it can store. To do this, add a colon and a space after the constant or variable name, followed by the type name. For example, to add a type annotation to the welcomeMessage
variable, indicating that it can store a String value:
var welcomeMessage: String
This line of code can be understood as: “declare a type String, named welcomeMessage
.” The String
type means “may store any value of type String.”
Constant and Variable Naming
You can use any character you like as a constant and variable name, including Unicode characters. However, constant and variable names cannot contain mathematical symbols, arrows, or retention (or illegal) Unicode code bits, and the connection tabs. They cannot start with a number, but can contain numbers elsewhere.
Once you have declared a constant or variable with a specific type, you cannot use the same name to declare another constant or variable with a different type. Additionally, you cannot use the same name for a constant and a variable.
If you need to use the same name as a keyword or constant variable name, you can use the backquote (`) to surround the key way to use it as a name.
Assigning Values to Variables
You can change an existing variable’s value to another value of the same type. For example, to change the value of the friendlyWelcome
variable from “Hello!” to “Bonjour!”:
var friendlyWelcome = "Hello!"
friendlyWelcome = "Bonjour!"
Note that attempting to change the value of a constant will result in a compilation error.
Outputting Values
You can use the println
function to output the current value of a constant or variable:
println(friendlyWelcome)
This will output “Bonjour!” to the console.
String Interpolation
Swift provides string interpolation, which allows you to insert the value of a constant or variable into a string. To do this, surround the constant or variable name with parentheses and use a backslash before the open parenthesis to escape:
println("The current value of friendlyWelcome is \(friendlyWelcome)")
This will output “The current value of friendlyWelcome is Bonjour!”
Comments
Swift provides two types of comments: single-line comments and multi-line comments. Single-line comments start with a double forward slash (//), while multi-line comments start with a single asterisk followed by a forward slash (/) and end with a single asterisk followed by a forward slash (/).
You can also nest multi-line comments in other multi-line comments.
Semicolons
Unlike most other programming languages, Swift does not require the use of semicolons at the end of each statement. However, you can still use semicolons if you prefer.
Integer Types
Swift provides several integer types, including Int8
, Int16
, Int32
, and Int64
. These types represent signed integers, while UInt8
, UInt16
, UInt32
, and UInt64
represent unsigned integers.
You can access the minimum and maximum values for each integer type using the min
and max
properties:
let minValue = UInt8.min
let maxValue = UInt8.max
Integer Range
You can access the minimum and maximum values for each integer type using the min
and max
properties:
let minValue = UInt8.min
let maxValue = UInt8.max
Type Safety and Type Inference
Swift is a type-safe language, which means that the compiler will prevent you from assigning a value of the wrong type to a constant or variable. Additionally, Swift provides type inference, which allows the compiler to automatically determine the type of a constant or variable based on its initial value.