1 Constants and variables
Constants and variables associate a name with a value of a specified type. The value of a constant cannot be changed once set, and the value of a variable can be changed at will.
1.1 Declare constants and variables
Constants and variables must be declared before use, let to declare constants, and var to declare variables.
let maximumNumberOfLoginAttempts = 10 // The maximum number of allowed login attempts
var currentLoginAttempt = 0 // Number of current login attempts
These two lines of code can be understood as:
Declare a new constant whose name is maximumNumberOfLoginAttempts and give it a value of 10. Then, declare a variable named currentLoginAttempt and initialize its value to 0. In this example, the maximum number of allowed login attempts is declared as a constant, because this value does not change. The current number of login attempts is declared as a variable, because this value needs to be increased each time the login attempt fails.
You can declare multiple constants or multiple variables on one line, separated by commas:
var x = 1.0, y = 2.0, z = 3.0
var x = 12, y = 22, z = 32
1.2 Type of annotation
When you declare a constant or variable, you can add a type annotation (type annotation), indicating the class of the value to be stored in the constant or variable
type. If you want to add a type label, you need to add a colon and a space after the constant or variable name, and then add the type name.
This example adds a type annotation to the welcomeMessage variable, indicating that this variable can store a value of type String:
var welcomeMessage: String
** The colon in the statement stands for "is ... type", so this line of code can be understood as:
"Declare a variable of type String and name welcomeMessage."
"Type is String" means "Any value of type String can be stored."
The welcomeMessage variable can now be set to any string: **
welcomeMessage = "Hello"
You can define multiple variables of the same type in one line, separated by commas, and add a type label after the last variable name:
var red, green, blue: Double
note:
Generally speaking, you rarely need to write type annotations. If you assign an initial value when declaring a constant or variable, Swift can infer this constant
The type of quantity or variable. In the above example, welcomeMessage was not given an initial
Value, so the type of the variable welcomeMessage is specified by a type annotation, not inferred by the initial value.
1.3 Naming of constants and variables
You can use any character you like as constant and variable names, including Unicode characters:
let π = 3.14159
let hello = "hello world"
let ?? = "dogcow"
Constant and variable names cannot contain mathematical symbols, arrows, reserved (or illegal) Unicode code points, connections and tabs. Can't open with numbers
Header, but you can include numbers elsewhere in constant and variable names.
Once you declare a constant or variable to a certain type, you cannot use the same name to declare again, or change the class of the value it stores
type. At the same time, you cannot convert constants and variables.
note:
If you need to use the same name as the Swift reserved keyword as a constant or variable name, you can use backticks (`) to surround the keyword
Use it as a name. In any case, you should avoid using keywords as constant or variable names.
You can change the existing variable value to other values of the same type. In the following example, the value of friendlyWelcome is changed from "Hello!" To "Bonj"
our! ":
var friendlyWelcome = "Hello!"
friendlyWelcome = "Bonjour!"
// friendlyWelcome is now "Bonjour!"
Unlike variables, the value of a constant cannot be changed once it is determined. Attempting to do so will result in an error at compile time:
let languageName = "Swift"
languageName = "Swift ++"
// This will report a compile-time error-languageName cannot be changed
1.4 Output constants and variables
You can use the print (_: separator: terminator :) function to output the current constant or variable value:
print (friendlyWelcome)
// prints "Bonjour!"
print (_: separator: terminator :) is a global function used to output one or more values to the appropriate output area
** Note: ** Swift uses string interpolation to add constant names or variable names as placeholders to long strings, and Swift will
Replace these placeholders with the current constant or variable values. Put the constant or variable name in parentheses and escape it with a backslash before the opening parenthesis:
print ("The current value of friendlyWelcome is \ (friendlyWelcome)")
// output "The current value of friendlyWelcome is Bonjour!
2. Comments 2.1 Single-line comments
The comments in Swift are very similar to the comments in C language. Single line comments start with a double forward slash (//):
// this is a comment
2.2 multi-line comments,
It starts with a single forward slash followed by an asterisk (/ *) and ends with an asterisk followed by a single forward slash (* /):
/* This is an,
Multi-line comments * /
2.3 Swift multi-line comments can be nested within other multi-line comments
/ * This is the beginning of the first multi-line comment
/ * This is the second nested multi-line comment * /
This is the end of the first multi-line comment * /
3 semicolon
Swift does not force you to use a semicolon (;) at the end of each statement. Of course, you can also add a semicolon according to your own habits. In one case, you must use a semicolon, that is, you plan to write multiple independent statements on the same line:
let cat = "?"; print (cat); var title: String;
// output "?"
4 integer
Integers are numbers without a fractional part, such as 42 and -23. Integers can be signed (positive, negative, zero) or unsigned (positive, zero).
Swift provides 8, 16, 32 and 64 bit signed and unsigned integer types. These integer types are similar to the naming method of C language, such as 8 bits
The unsigned integer type is UInt8 and the 32-bit signed integer type is Int32. Like other types in Swift, integer types are named in uppercase
law.
4.1 Integer range
You can access the min and max properties of different integer types to get the minimum and maximum values of the corresponding types:
let minValue = UInt8.min // minValue is 0, which is of type UInt8
let maxValue = UInt8.max // maxValue is 255, which is of type UInt8
The type of the value returned by min and max is exactly the type of the integer it returns (such as UInt8 in the above example, the type returned is UInt8), which can be used in expressions
Next to values of the same type.
4.2 Int
In general, you do not need to specify the length of an integer. Swift provides a special integer type Int whose length corresponds to the native word length of the current platform
with:
? On 32-bit platforms, Int and Int32 are the same length.
? On 64-bit platforms, Int and Int64 are the same length.
Unless you need integers of a specific length, generally speaking Int is sufficient. This can improve code consistency and reusability. Even on a 32-bit platform
Above, the range of integers that Int can store can also reach -2,147,483,648 ~ 2,147,483,647, which is large enough most of the time.
4.3UInt
Swift also provides a special unsigned type UInt with the same length as the native word length of the current platform:
? On 32-bit platforms, UInt and UInt32 are the same length.
? On 64-bit platforms, UInt and UInt64 are the same length.
note:
Try not to use UInt unless you really need to store an unsigned integer that is the same as the native word length of the current platform. Except in this case, it is better to use Int even if the value you want to store is known to be non-negative. Uniform use of Int can improve code reusability, avoid conversion between different types of numbers, and match type inference of numbers.
5. Floating point numbers
Floating point numbers are numbers with a decimal part, such as 3.14159, 0.1 and -273.15.
Floating-point types have a larger range than integer types, and can store numbers that are larger or smaller than Int types. Swift provides two signed floating point numbers
Types of:
? Double represents a 64-bit floating point number. Use this type when you need to store large or very high precision floating point numbers.
? Float represents a 32-bit floating point number. This type can be used when accuracy is not required.
note:
Double is highly accurate, with at least 15 digits, and Float has at least 6 digits. Which type to choose depends on what your code needs to deal with
The range of values.
6. Type safety and type inference
Swift is a type safe language. A type-safe language allows you to clearly know the type of value that the code will handle. If your code requires a String, you absolutely cannot accidentally pass in an Int. Because Swift is type-safe, it will perform type checks when you compile your code and mark the types that do not match
Is wrong. This allows you to discover and fix errors as early as possible during development.
When you want to deal with different types of values, type checking can help you avoid errors. However, this does not mean that you need to specify the type explicitly each time you declare constants and variables. If you don't specify the type explicitly, Swift will use type inference to select the appropriate type. With type inference, the compiler can automatically infer the type of an expression when compiling code. The principle is simple, just check the value you assign.
Because of type inference, Swift rarely needs to declare types compared to C or Objective-C. Although constants and variables need to be clearly typed, most of the work does not require you to do it yourself. Type inference is very useful when you declare constants or variables and assign initial values. When you declare constants or variables, assign them a literal value (literal value or literal) to trigger type inference. (Literal values are the values that will appear directly in your code, such as 42 and 3.14159.)
For example, if you assign a value of 42 to a new constant and do not indicate the type, Swift can infer that the constant type is Int because of the initial value you assigned to it
The value looks like an integer:
let meaningOfLife = 42
// meaningOfLife will be presumed to be of type Int
Similarly, if you don't specify a type for a floating-point literal, Swift will infer that you want Double:
let pi = 3.14159
// pi will be presumed to be of type Double
When inferring the type of floating point numbers, Swift always chooses Double instead of Float.
If both integer and floating point appear in the expression, it will be inferred to be of type Double:
let anotherPi = 3 + 0.14159
// anotherPi will be presumed to be of type Double
The original value 3 did not explicitly declare the type, and a floating-point literal appeared in the expression, so the expression would be inferred to be of type Double.
7. Numeric literals
The whole number can be written as:
? A decimal number without a prefix
? A binary number with the prefix 0b
? An octal number with the prefix 0o
? A hexadecimal number, the prefix is 0x
The decimal values of all integer literals below are 17:
let decimalInteger = 17
let binaryInteger = 0b10001 // binary 17
let octalInteger = 0o21 // 17 in octal
let hexadecimalInteger = 0x11 // 17 in hexadecimal
Floating-point literals can be decimal (no prefix) or hexadecimal (prefix is 0x). There must be at least one decimal number on both sides of the decimal point
Word (or hexadecimal number). Floating-point literals also have an optional exponent (exponent, through uppercase or lowercase in decimal floating-point numbers)
It is specified by the written e, and specified by uppercase or lowercase p in the hexadecimal floating point number.
If the exponent of a decimal number is exp, Then this number is equivalent to the product of the base and 10 ^ exp: * 1.25e2 means 1.25 × 10 ^ 2, which is equal to
125.0. * 1.25e-2 means 1.25 × 10 ^ -2, which is equal to 0.0125.
If the exponent of a hexadecimal number is exp, then this number is equivalent to the product of the base and 2 ^ exp: * 0xFp2 means 15 × 2 ^ 2, which is equal to 6
0.0. * 0xFp-2 means 15 × 2 ^ -2, which is equal to 3.75.
The following floating-point literals are all equal to 12.1875 in decimal:
let decimalDouble = 12.1875
let exponentDouble = 1.21875e1
let hexadecimalDouble = 0xC.3p0
Numeric literals can include additional formats to enhance readability. Both integers and floating-point numbers can add extra zeros and contain underscores, and will not affect
The literal volume:
let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_000_1
8. Numeric type conversion
In general, even if integer constants and variables in the code are known to be non-negative, use the Int type. Always use the default integer type to guarantee your
Integer constants and variables can be directly reused and can match the type inference of integer class literals.
Only use other integer types when necessary, for example, to process external data with a clear length or to optimize performance and memory usage
Wait. The use of explicitly specified length types can promptly discover value overflows and can imply that special data is being processed.
8.2 Integer conversion
Variables and constants of different integer types can store different ranges of numbers. The range of numbers that Int8 type constants or variables can store is -12
8 ~ 127, and the numeric range that UInt8 type constants or variables can store is 0 ~ 255. If the number exceeds the constant or the variable can be stored
Scope, error will be reported when compiling:
let cannotBeNegative: UInt8 = -1
// The UInt8 type cannot store negative numbers, so an error will be reported
let tooBig: Int8 = Int8.max + 1
// The Int8 type cannot store the number exceeding the maximum value, so an error will be reported
Since each integer type can store a different range of values, you must selectively use numeric type conversion according to different situations. This selectivity
The method used can prevent implicit conversion errors and make the type conversion intent in your code clear.
To convert one number type to another, you need to use the current value to initialize a new number of the desired type, the type of this number is your goal
Types of. In the following example, the constant twoThousand is of type UInt16, while the constant one is of type UInt8. They cannot be directly related
Plus, because they are different types. So call UInt16 (one) to create a new UInt16 number and initialize it with the value of one, then make
Use this new number to calculate:
let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16 (one)
Now both types of numbers are UInt16 and can be added. The type of the target constant twoThousandAndOne is inferred as UInt1
6 because it is the sum of two UInt16 values.
SomeType (ofInitialValue) is the default method to call the Swift constructor and pass in an initial value. Within the language, UInt16 has a structure
The builder can accept a value of type UInt8, so this constructor can use the existing UInt8 to create a new UInt16. Note that you
You can't pass in any type of value, only the value of the corresponding constructor inside UInt16. But you can extend the existing type so that it can receive
Other types of values (including custom types).
8.3 Integer and floating point conversion
The conversion of integers and floating-point numbers must explicitly specify the type:
let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double (three) + pointOneFourOneFiveNine
// pi is equal to 3.14159, so it is presumed to be of type Double
In this example, the value of the constant three is used to create a value of type Double, so the number types on both sides of the plus sign must be the same. If you do not transfer
In other words, the two cannot be added.
The reverse conversion of floating-point numbers to integers is the same line. Integer types can be initialized with Double or Float types:
let integerPi = Int (pi)
// integerPi is equal to 3, so it is presumed to be of type Int
When a new integer value is initialized in this way, the floating-point value will be truncated. In other words, 4.75 will become 4 and -3.9 will become -3.
note:
Combining numeric constants and variables is different from combining numeric literals. Literal 3 can be added directly to literal 0.14159, because digital literal
There is no clear type. Their types are only inferred when the compiler requires a value.
9 type aliases
Type aliases define another name for an existing type. You can use the typealias keyword to define type aliases.
Type aliases are very useful when you want to give existing types a more meaningful name. Suppose you are dealing with the number of external resources of a certain length
according to:
typealias AudioSample = UInt16
After defining a type alias, you can use the alias anywhere the original name is used:
var maxAmplitudeFound = AudioSample.min
// maxAmplitudeFound is now 0
In this example, AudioSample is defined as an alias for UInt16. Because it is an alias, AudioSample.min is actually UInt16.mi
n, so maxAmplitudeFound will be assigned an initial value of 0.
10 Boolean (Bool)
Swift has a basic Boolean type called Bool. Boolean values refer to logical values because they can only be true or false. Swift has two boolean constants, true and false:
let orangesAreOrange = true
let turnipsAreDelicious = false
The types of orangesAreOrange and turnipsAreDelicious are inferred as Bool because their initial value is a boolean literal. Just like the Int and Double mentioned earlier, if you assign them true or false when you create a variable, then you don't need to declare the constant or variable as type Bool. When the value type assigned to a constant or variable is known, you can trigger type inference, which makes Swift code more concise and more readable. Boolean values are very useful when you write conditional statements such as if statements:
if turnipsAreDelicious {
print ("Mmm, tasty turnips!")
} else {
print ("Eww, turnips are horrible.")
}
// prints "Eww, turnips are horrible."
Conditional statements, such as if
If you use a non-Boolean value where you need to use the Bool type, Swift's type safety mechanism will report an error. The following example will report a compilation
Time error:
let i = 1
if i {
// This example will not be compiled and will report an error
}
However, the following example is legal:
let i = 1
if i == 1 {
// This example will compile successfully
}
The comparison result of i == 1 is of type Bool, so the second example can pass the type check. A comparison like i == 1. As with other type-safe examples in Swift, this method can avoid errors and ensure that the intention of this piece of code is always clear.
11. Tuple
Tuples combine multiple values into a composite value. The values in the tuple can be of any type, and are not required to be of the same type.
In the following example, (404, "Not Found") is a tuple describing the HTTP status code. HTTP status
The code is a special value returned by the web server when you request a web page. If the page you requested does not exist, it will return a 404 Not Foun
d Status code.
let http404Error = (404, "Not Found")
// The type of http404Error is (Int, String), the value is (404, "Not Found")
(404, "Not Found") tuple combines an Int value and a String value to represent two parts of the HTTP status code: a number and a
A human-readable description. This tuple can be described as "a tuple of type (Int, String)".
You can combine types in any order into a tuple. This tuple can contain all types. As long as you want, you can create a type of (Int, I
nt, Int) or (String, Bool) or any other combination of tuples you want.
You can decompose the contents of a tuple into separate constants and variables, and then you can use them normally:
let (statusCode, statusMessage) = http404Error
print ("The status code is \ (statusCode)")
// prints "The status code is 404"
print ("The status message is \ (statusMessage)")
// prints "The status message is Not Found"
If you only need a part of the tuple value, you can mark the part to be ignored with an underscore (_) during decomposition:
let (justTheStatusCode, _) = http404Error
print ("The status code is \ (justTheStatusCode)")
// prints "The status code is 404"
In addition, you can also access individual elements in the tuple through subscripts, which start from zero:
print ("The status code is \ (http404Error.0)")
// prints "The status code is 404"
print ("The status message is \ (http404Error.1)")
// prints "The status message is Not Found"
You can name individual elements when defining tuples:
let http200Status = (statusCode: 200, description: “OK”)
After naming the elements in the tuple, you can get the values of these elements by name:
print ("The status code is \ (http200Status.statusCode)")
// output "The status code is 200"
print ("The status message is \ (http200Status.description)")
// prints "The status message is OK"
Tuples are very useful when returning values as functions. A function used to fetch web pages may return a (Int, String) tuple to describe whether to fetch
success. Compared with a value that can only return one type, a tuple containing two different types of values can make the function return information more useful. Please see
Examine the function parameters and return value (page 0).
note:
Tuples are useful when temporarily organizing values, but they are not suitable for creating complex data structures. If your data structure is not used temporarily, please make
Use classes or structures instead of tuples. Please refer to classes and structures.
10. Optional types 10.1nil
You can assign an optional variable to nil to indicate that it has no value:
var serverResponseCode: Int? = 404
// serverResponseCode contains an optional Int value 404
serverResponseCode = nil
// serverResponseCode now contains no value
note:
nil cannot be used for non-optional constants and variables. If there are constants or variables in your code that need to handle missing values, please declare them in pairs
Should be optional.
If you declare an optional constant or variable but no value is assigned, they will be automatically set to nil:
var surveyAnswer: String?
// surveyAnswer is automatically set to nil
11. Error handling
You can use error handling to deal with error conditions that may be encountered during program execution.
Relative to the optional use of the presence and absence of values to express the success and failure of the function, error handling can infer the cause of the failure and propagate it to the program ’s other
He part.
When a function encounters an error condition, it can report an error. Where the function is called, an error message can be thrown and handled reasonably.
func canThrowAnErrow () throws {
// This function may throw an error
}
A function can throw an error message by adding the throws keyword to the declaration. When your function can throw an error message, you should be in the expression
Try keyword in front.
do {
try canThrowAnErrow ()
// No error message is thrown
} catch {
// An error message is thrown
}
A do statement creates a new include scope, allowing errors to be propagated to one or more catch clauses.
Here is an example of how error handling can be used to deal with different error conditions.
func makeASandwich () throws {
// ...
}
do {
try makeASandwich ()
eatASandwich ()
} catch Error.OutOfCleanDishes {
washDishes ()
} catch Error.MissingIngredients (let ingredients) {
buyGroceries (ingredients)
}
In this example, the makeASandwich () (make a sandwich) function throws an error message if there is no clean plate or a raw material is missing
Lost. Because makeASandwich () throws an error, the function call is wrapped in a try expression. Wrap the function in a do statement, any
The thrown error will be propagated to the provided catch clause.
If no error is thrown, the eatASandwich () function will be called. If an error matching Error.OutOfCleanDishes is thrown, the washDishes function will be called. If an error that matches Error.MissingIngredients is thrown, the buyGroceries (_ :) function will be called with the associated value [String] captured by catch. Throwing, catching, and spreading errors will be handled in error.
Learning address
Swift learning first shot