One, operator
Some of the operators supported by 1.Swift have the following
Assignment operator: =
Compound Assignment operators: + =,-=
Arithmetic operators: + 、-、 *,/
Remainder Operator:%
Self-increment, decrement operator: + + 、--
Comparison operators: = =,! =, >, <, >=, <=
Logical operators:&&, | |,!
Trinocular operator:? :
Range operators:.. < 、...
Overflow operators: &+, &-, &*, &/, &%
2. Assignment operators
(1) 1 to 1 assignment
var a = 5
Let B = 20
(2) N assignment to n (tuple)
Let (x, y) = (1, 2)//x is the value of 1,y is 2 and is constant
Note: Unlike C\oc, the assignment operator for Swift does not return a value
if (x = y) {}
The above code is wrong because x = y does not return a specific value
Description: This benefit is to prevent misuse = and = =
3. Arithmetic operators
Simple to use
1 + 2//3
5-3//2
2 * 3//6
10.0/2.5//4.0
"HTTP//" + "www.wendingding.cn"//"http://www.wendingding.cn"
4. Finding the remainder operator
% is called "redundancy operator" in Swift, and language is called "modulo operator".
9% 4//1
-9% 4//-1
9%-4//1
-9%-4//-1
Hint: the positive and negative of the remainder result is the same as the positive value of the left
Note: Unlike the C language, Swift's% supports calculation of floating-point numbers
8% 2.5//0.5
5.Bool type
The bool type, also known as the logical type (Logical), takes 2 values
True: True
False: False
Note: In the C language: 0 is false, not 0 is true, and there is no such concept in Swift
The condition of the IF statement must be a value of type bool
(1) Wrong wording
if (10) {
println ("conditions established")
}
(2) Correct wording
if (true) {
println ("conditions established")
}
6. Three mesh operator
The comparison operator \ Logical operator returns a value of type bool with a value of 2 possible
True: True, 6 > 5, (7 > 6) && (9! = 7)
False: False, 6 < 5, (7 >= 6) && (9 = = 7)
The condition of the trinocular operator must be a value of type bool
(1) Wrong wording
Let A = 10
Let C = a? 100:200
(2) Correct wording
Let c = a! = 0? 100:200
Let C = False? 100:200
7. Scope operators
The range operator is used to represent a range with 2 types of range operators
Closed range Operator: a...b, representing [A, b], containing A and b
Semi-closed range operator: A.. <b, which means [a, b], contains a, does not contain B
Example:
The value of index from
The value of index from 1~4
8. Overflow operator
(1) Brief description
Each data type has its own range of values, which, by default, generates a compile or run-time error once a value that exceeds the range of values is assigned
Note: The following notation is incorrect
Let x = Uint8.max
Let y = x + 1
The 2nd line of code will run the Times wrong (run-time error)
Tip: The above is a run-time error, and the following error is a compile-time error.
Swfit provides 5 & start overflow operators for integer calculations that can be flexibly processed for values beyond the range of values
overflow addition &+
Overflow subtraction &-
Overflow multiplication &*
Overflow Division &/
Overflow to seek residual &%
(2) Overflow of values
Let x = Uint8.max
Let y = x &+ 1
After the 1th line of code: The value of x is 255 (maximum)
After the 2nd line of code: The value of y is 0 (minimum value)
(3) Underflow of the value
Let x = Uint8.min
Let y = x &-1
After the 1th line of code: The value of x is 0 (minimum value)
After the 2nd line of code: The value of Y is 255 (maximum)
Signed integers also have similar overflow phenomena
Let x = Int8.min
Let y = x &-1
After the 1th line of code: The value of X is-128 (minimum value)
After the 2nd line of code: The value of Y is 127 (maximum)
(4) Except 0 overflow
By default, a number is removed from 0, or the remainder is calculated for 0, and the compiler will directly error
Let x = 10
Let y = x/0
Let z = x% 0
2nd, 3 lines of code: the compiler will error
If you use the overflow operator, you will not get an error
Let x = 10
Let y = x &/0
Let z = x &% 0
The values of Y and Z are ultimately 0.
Second, assert
1. Brief description
Assertion is a method for real-time detection of whether the condition is true
If the condition is true, then the code continues to execute
If the condition is false, an error message is thrown to terminate the program's operation directly.
2. Usage of assertions
Using the global Assert function
The ASSERT function receives a BOOL expression and a message that appears when an assertion fails
ASSERT (Index >= 0, "Index must be greater than 0")
If index is greater than or equal to 0, continue with the following code
If index is less than 0, the error message (the black word below) is thrown, terminating the program's operation directly
Assertion Failed:index must be greater than 0
ASSERT (index >= 0)
Error messages can be omitted, but not recommended, which is not conducive to debugging
3. Usage scenarios and Notes
(1) The following scenario may be used to assert
Index of the instrumented array: cannot be too small or too large to cause the array to cross out
To detect arguments passed to a function: if it is an invalid parameter, it cannot be used in the function.
(2) Notice of use of assertions
Assertions can cause the program to abort, and if the condition is true, continue to execute code, you cannot use assertions
Assertions can ensure that errors are discovered in the development process, but it is best not to use them in published applications.
If a program suddenly crashes with the use of a utility, it can seriously affect the user experience
iOS Development Swift-(four) operator