Loops For conditional increment statement
1 for (var counter = 0; counter <8; counter ++)
2 {
3 liftWeights ()
4}
The syntax is this: use for as the start of the loop, tell Xcode that you are declaring a loop, and for is followed by parentheses, which declare variables, conditions, and increment values. E.g:
1 for (VARIABLE; CONDITION; INCREMENT)
2 {
3
4}
The first part in parentheses is a variable, which is represented by counter. It counts the number of loops that have been completed. When writing a program in normal times, the variable here is often named counter (counter has the meaning of a counter in English) and then the initial value zero:
1 for (var counter = 0; CONDITION; INCREMENT)
2 {
3
4}
The semicolon after the condition is followed by the increment value, which is the change of the variable counter after each loop:
1 for (var counter = 0; counter <8; counter ++)
2 {
3
4}
However, Apple provides a shorthand way to use two plus signs ++ to represent this variable plus 1, for example:
counter ++
The effect is the same as this:
counter = counter +1
Ranges
The interval Range is similar to a number in the integer array. There are currently two types of intervals. One is a closed interval, which is represented by three dots, including the numbers on both sides of the dot:
1 ... 5 // 1,2,3,4,5
The other is a half-closed half-open interval, which is represented by two small dots plus a less than sign. The number to the right of the less than sign is not included in this interval:
1 .. <5 // 1,2,3,4
In a for-in loop, you can use intervals instead of arrays or dictionaries:
1 for index in 1 ... 5
2 {
3 println ("The current number is \ (index)")
4}
5
6 // The print result is:
7 // The current number is 1
8 // The current number is 2
9 // The current number is 3
10 // The current number is 4
11 // The current number is 5
Conditional expression
1 if isBirthdayToday == true
2 {
3 singBirthdaySong ()
4}
In the above example, the condition is isBirthdayToday == true, the two equal signs indicate comparing the values between the two equal signs, if the values are the same, the result is true, if the values are not the same, the result is false.
Optionals
Optional values are used to handle variables that may have null values. In some cases, you cannot be sure that a variable has a value. For example, a word in Spanish may not be directly translated into a word in English, so a null value will appear. This situation without a value is called nil.
Optional values can be used in any type of variable. When used, a question mark follows the type to indicate that this is an optional value:
1 var translatedWord: String?
Because variables that may be empty must be represented by names, this ensures that all non-optionally valued variables will have values. This design pattern helps developers avoid program crashes caused by null values. Non-optional value variables must have values. Optional value variables can have no value.
Optional values cannot be used directly, they need to be unwrapped before use. Think of using an optional value variable as unpacking a bag of candy, and you must tear off the package before you can eat the candy. When an optional value variable is unpacked, the variable may also be null. This is the equivalent of breaking up a candy and finding nothing inside.
The unpacking process helps developers remember to check and ensure that this variable is not null. There are two steps to using optional values. The first step is to check if it is null. Generally, use if expressions to check:
1 var translatedWord: String? = Translate ("cat")
2 if translatedWord! = Nil {
3 // translatedWord has a value
4} else {
5 // The translatedWord has no value
6}
View Code
Once the check is indeed valuable, you must unpack it. Unpacking an optional value is very simple, just put an exclamation mark after the variable, for example:
1 var translatedWord: String? = Translate ("cat")
2 if translatedWord! = Nil {
3 println (translatedWord!) // gato
4}
View Code
There will be some confusion and unaccustomedness when you first come into contact with optional values. In fact, you just need to remember that a variable that may be empty must be an optional value. When the optional value is empty, it is called nil.
Swift Understanding (2)