We’ll focus on the common and advanced swift operators, how to automatically shift operators and implement standard operators for our defined types.
Assignment operator
The assignment operator (a = b) initializes or updates the value of a with the value of b:
let b = 5
var a = b
Copy the code
If the right-hand side of the assignment is a tuple with multiple values, its elements can be decomposed into multiple constants or variables at once:
let (x, y) = (10, 2)
Copy the code
Unlike C and OC, assignment does not reverse. The following code is invalid
if a = b {
}
Copy the code
Arithmetic operator
Swift supports four basic arithmetic types: +-*/
// 1 + 2 // 5-4 // 6 * 7 // 8/9Copy the code
Swift also supports string addition
"hello, " + "world" // hello, world
Copy the code
Remainder operator
The remainder operator (a % b) calculates how many multiples of A fit b and returns the remainder (called the remainder).
let a = 10%4
print("\(a)") // 2
Copy the code
Compound assignment operator
Like C, Swift provides the compound assignment operator, which combines an assignment (=) with another operation.
// +=
var a = 1
a += 2 // 3
// -=
var a = 3
a -= 1 // 2
Copy the code
Comparison operator
Swift supports the following comparison operators
- Equal (a == b)
- Different (a! = b)
- Greater than (A > b)
- Less than (A < b)
- A >= b
- A <= b)
Var a = 4 var b = 4 if a == b {print("ab = b ")} b = 5 if a! = b {print("a < b")} if b > a {print("a < b")} if b > a {print("b < b")} if a <= b {print("a < b")} b = 4 if a >= b {print("a < b")} b = 4 if a >= b B {print("a = 4")}Copy the code
Ternary conditional operator
Is the ternary operation in the form of a Boolean condition? True result 1: Flase result 2
var a = 4 var b = 4 let c = a == b ? "Equal" : "not equal" // EqualCopy the code
Zero merge operator
Nil merge operator (a?? B) Unpack an optional a if it contains a value, or return a default b if A is nil. Expression A is always optional. Expression B must match the type stored in A.
The zero merge operator is short for:
a ! = nil ? a! : bCopy the code
The above code uses the ternary conditional operator and forced unpacking (a!). Access the value wrapped in a when A is not nil, otherwise return b. The zero-merge operator provides a more elegant way to encapsulate this conditional checking and expansion in a concise and readable form.
let defaultColorName = "red" var userDefinedColorName: String? Var colorNameToUse = userDefinedColorName?? defaultColorName // redCopy the code
Range operator
Swift contains several range operators, which are shortcuts to expressing a set of values.
Closed range operator
a… B defines a range of values from a to B, including a and B, a cannot be greater than b, and half is used with for in
for a in 1... 5 { print("\(a)") // 1 // 2 // 3 // 4 // 5 }Copy the code
Half-open range operator
The half-open range operator (a.. <b) defines the range from A to B, but not b. It is said to be half open because it contains its first value but not its final value. As with the closed interval operator, the value of a must not be greater than b. If the value of A is equal to b, the result range will be empty. We can usually use it to expand an array
let array = ["1","2","3","4","5"] for i in 0.. <array.count { print(array[i]) // 1 // 2 // 3 // 4 // 5 }Copy the code
Scope of unilateral
A direction that continues as far as possible in a direction, such as an array from the beginning to index or index to all the elements at the end of the array. In these cases, you can omit the value on the side of the range operator. This range is called a one-sided range because the operator has a value on only one side.
/ / traverse index 0 start until the end let all the elements of an array = [" 1 ", "2", "3", "4", "5"] for a in array [0]... {print (a) / / 1 / / 2/3/4 / / / 5} / / traverse to the subscript 2 let all the elements of an array = [" 1 ", "2", "3", "4", "5"] for a array in [2]... { print(a) // 1 // 2 // 3 }Copy the code
Half open unilateral range
Half open one-side range is also a one-side range, the difference is that it is not easy to call the index element
/ / can iterate through the set of all elements within the array = [" 1 ", "2", "3", "4", "5"] for a in array [.. < array. Count] {print (a) / / 1 / / 2 / / 3/4 / / 5}Copy the code
Logical operator
Swift mainly contains the following three standard logical operators
- Logic is not a!
- Logic and a && B
- Logic or a | | b
Logical non-operator
Change Boolean to false and false to true
let a = true var b = ! B =! b // trueCopy the code
Logic and
Logic and that means that the Boolean is true when both the && and the left and right are true, one true and one false are false, both are false and the result is false
true && true // true
true && false // false
false && false // false
Copy the code
Logic or
On both sides of the logic or refers to the | | as long as there is a true result is true, only to false when the result is false
true || true // true
true || false // true
false || false // false
Copy the code
Combinatorial logical operators
Swift logical operators && and | | is the left associated, which means that the compound expressions with multiple logical operators first evaluate the leftmost subexpression.
A = false b = true c = false d = true a && b | | c | | d / / decomposition combinational logic operator ab = a && b / / false ABC = ab | | c / / false abcd = ABC || d // trueCopy the code
When using combinatorial logic operators, we can use parentheses to express the priority, making it easier to read the code.