1. In swift and Java basic operator syntax, assignment operators, arithmetic operators, remainder operators, self-increment and decrement operations, unary minus operators, unary plus operators, logical operations, comparison operators, trinocular operators (ternary Conditional Operator) These operator usages are similar, close to the same, and are not analyzed, and some of the syntax specific to Swift is analyzed below.
2. Empty-fit operators
The empty-fit operator (Nil coalescing Operator): The Empty-fit operator ( a ?? b
) a
makes a null judgment on the optional type, and if a
it contains a value, it is returned with a default value b
. This operator has two conditions:
- The expression
a
must be of type optional
b
the type of the default value must be a
consistent with the type of the stored value
The following example uses an empty merge operator to make a choice between the default color name and the optional custom color name, as follows:
" Red " var userdefinedcolorname:string? // The default value is nilvar colornametouse = userdefinedcolorname?? Defaultcolorname // the value of Userdefinedcolorname is empty, so the value of Colornametouse is ' red '
userDefinedColorName
The variable is defined as an optional string type, and the default value is nil. Because userDefinedColorName
it is an optional type, we can use the null-fit operator to determine its value. In the previous example, a variable named by the null-fit operator is colorNameToUse
given a string type initial value. Because the userDefinedColorName
value is empty, the expression userDefinedColorName ?? defaultColorName
returns the default value, which is red
.
In another case, assign a non-null value ( non-nil
) to userDefinedColorName
, perform an empty operation again, and the result is the value of the packet in, userDefaultColorName
rather than the default value, the code is as follows:
" Green " = userdefinedcolorname?? Defaultcolorname // Userdefinedcolorname is not empty, so the value of Colornametousede is green
3. Interval operators
Interval operator: Swift provides two operators that facilitate the expression of a range of values.
Closed interval operator: The closed interval operator ( a...b
) defines an a
b
interval that contains all values from to (including a
and b
), which b
must be greater than a
. The closed interval operator is useful when iterating through all the values of an interval, such as in for-in
loops, where the code is as follows:
for i in 1 ... 5 {print ( i = \ (i) * 5 = \ (i * 5) )} // i = 1 * 5 = 5 // i = 2 * 5 = ten // i = 3 * 5 = // i = 4 * 5 = // i = 5 * 5 =
Half open interval operator: Half open interval ( a..<b
) defines a a
range from to b
but not included b
. The semi-open interval is called because the interval contains the first value and does not include the last value. The practicality of the semi-open interval is that when you use a 0-based list (such as an array), it is very convenient to count from 0 to the length of the list. The code is as follows:
var names: [String] = ["Anna","Alex","Brian","Jack"] Let Count=Names.count forIinch 0.. <count{Print ("First \ (i) name \ (Names[i])") }//The 1th person is Anna.//2nd Guy's name is Alex.//3rd Guy's name is Brian.//The 4th Guy's name is Jack.
Comparison of Swift and Java in basic operators