Operators are special symbols or phrases that check, change, merge values,
Swift supports most of the standard C-language operators and improves many features to reduce general coding errors.
/*
Such as: the assignment (=) does not return a value to prevent errors caused by assigning an assignment to the place where you want to determine the equality operator (= =). Arithmetic operator (+,-, *,/,%) It is not allowed to wait for a value overflow to avoid saving the variable because the variable is larger or less than the result of the range of its type, which, of course, allows you to use Swift's overflow operator to implement overflow.
Swift also provides a C language that does not have an interval operator (a) that expresses a value between two numbers. <b and A...B), which facilitates the expression of values within a range
*/
Terms
/*
The operator is divided into one dollar. Binary and ternary operators:
1. Unary operators operate on single-action objects (such as-a). Unary operators are pre-and post-operators, and the predecessor operator needs to be immediately before the operand (such as!b), and the post operator needs to follow the action object (for example: c!)
2. The two-dollar operator operates two operands (for example, 2 + 3), which are central because they appear between two operand objects
3. Ternary operator operation three operands and C language, Swift has only one ternary operator, which is the three-mesh operator (a? b:c)
*/
Assignment operator (a = b), which represents the value of B to initialize or update the value of a
If the right side of the assignment is a multivariate group, his and the elements can be immediately decomposed into multiple constants or variables.
Unlike the C language and OC, Swift's assignment operation returns any value, so the code is wrong
/*
If x = y {
This sentence error because x = y does not return any values
}
This feature makes it impossible for you to write (=). Swift will help you with your nose. Such errors
*/
Arithmetic operators
All values in swift support basic four arithmetic operators
Unlike the C and OC languages, Swift by default does not allow one occurrence in numeric operations. But you can use Swift's overflow operator to implement overflow operations (such as: a &+ B)
The addition operator can also be used for concatenation of strings such as "Hello" + "World"
Remainder operator
The remainder operator (a% b) is calculated how many times B is just good enough to be able to accommodate a, returning a portion of the rest of the excess)
Attention:
Find remainder operator (%) In other languages it is also called the ' modulo operator ', but strictly speaking, we look at the result of the operator's operation on negative numbers, [redundancy] better
When a negative number B is found, the B symbol is ignored, which means that the result of a% B and a%-B is the same
unary minus operator (-) is written before the operand with no spaces in the middle
Combining assignment operators
The combination operator is the same as the C language combination operator, but there is no return value let B = a + = 2 It is wrong to write
Comparison operators
All standard C-language comparison operators can be used in swift
/*
equals (A = = b)
Not equal to (a! = b)
Greater than (a > B)
Less than (a < b)
Greater than or equal to (a >= b)
Less than or equal to (a <= b)
Attention:
Swift also provides a constant equal to (= = =) and not constant Equals (!==) to determine whether two objects are referenced by the same object instance
Each comparison operator returns a Boolean value in which another identity expression is true
1 = = 1//True, because 1 equals 1
2! = 1//True because 2 is not equal to 1
2 > 1//True because 2 is greater than 1
1 < 2//True because 1 is less than 2
1 >= 1//True because 1 is greater than or equal to 1
2 <= 1//False, because 2 is not less than or equal to 1
Comparison operators are used for conditional statements
*/
You can also use these operators to compare their sizes when the values in a tuple can be compared, for example, because the values of int and string can be compared, so tuples of type (int, string) can also compare size, whereas Bool cannot be compared, it also means that there is Tuples with Bool cannot be compared
Compare the size of a tuple to a left-to-right, per-value comparison until two values are found to stop, and if all values are equal then this pair of tuples we think they are equal
Attention:
The SWIFT standard library can only compare seven elements of a tuple comparison function, if your tuple element exceeds the number of seven elements, you need to implement the comparison operator
Trinocular operator
/*
The special of the trinocular operator is that it is an operator with three operands, and his form is a problem? Answer 1: Answer 2. He succinctly expressed the question based on whether to make a two-choice operation, if the problem is true, return to answer 1, and return to the result of answer 2
*/
Empty-fit operators
The empty-fit operator (a?? b) Determines whether an optional type A is null, if a contains a value to be unpacked, or returns a default value of B, the expression a must be an optional type (optional), and the default value B must be consistent with the type of a stored value
The empty-fit operator is a short expression of the code
A! = nil? A! : b
The code above uses the three-mesh operator. When the value of the optional type A is not empty, forced a! is used to access the value of a, whereas the value of the default value B is returned, and the null-fit operator provides a more elegant way to encapsulate conditions to determine and seal two behaviors, which is concise and more readable.
Attention:
If a is a non-null value (Non-nil), then B will not be evaluated, which is called short-circuit evaluation.
For example
Let Defaultcolorname = "Red"
var userdefinedcolorname:string? The default value is nil
var colornametouse = userdefinedcolorname?? Defaultcolorname
The value of Userdefinedcolorname is empty, so the value of Colornametouse is "red"
In another case, assign a non-null value (Non-nil) to Userdefinedcolorname and perform an empty operation again, and the result is the value of the packet in Userdefinedcolorname, not the default value.
Userdefinedcolorname = "Green"
Colornametouse = Userdefinedcolorname?? Defaultcolorname
Userdefinedcolorname is not empty, so the value of Colornametouse is "green"
Interval operator (range Operators)
Swift provides two interval operators that facilitate the expression of a range of values
Closed interval operator
The Closed interval operator (A...B) defines an interval that contains all values from A to B (including A and b), and the value of a cannot exceed B
Semi-open interval operator
Semi-open interval operator (a). <B) defines a range from a to B but not including b
The semi-open interval is called because the interval contains the first value and does not contain the last value
The practicality of the semi-open interval is that when you use a 0-based list (such as 0), it is very convenient to get from 0 to the list length
Logic and operators
When the logical operators (a && B) express that only the values of A and B are true, the value of the entire expression is true, so long as any one value is false, the value of the entire expression is false, in fact, if the first value is false, that You do not need to calculate the second value, because he has not been able to affect the results of the entire expression, which is called short-circuit calculation
Logical OR operator
Logical OR operator (a | | b) is a by two consecutive | The middle operator that is composed. It indicates that one of the two logical expressions is true, and the entire expression is true.
Similar to logical AND operator, logical OR "short-circuit", when the left-hand expression is true, the right expression is not evaluated because it is not possible to change the value of the entire expression.
Combination calculation of logical operators
Attention:
Swift logic operators && and | | is left-associative, which means that a compound expression with a multivariate logical operator takes precedence over the leftmost sub-expression.
Use parentheses to clarify precedence
For a complex expression to be easier to understand, it is very effective to use parentheses in a suitable place to clarify precedence, although he is not necessary,
Swift Learning-03--Basic operator