SoFunction
Updated on 2025-04-11

Summary of iOS Swift logical operator examples

Operator classification

From the perspective of operands: operators include one, two, and three. Here, one, two and three refer to the number of operands, and operands refer to the value being operated.

From the perspective of operator position: operators are divided into prefix, infix, and suffix. For example: !b, b + c, c!

Assignment operator

The assignment operator (a = b) can initialize or update the value of a  to b :

If the assignment symbol is on the right side of the tuple with multiple values, its elements will be split into constants or variables at once:

let (x, y) = (1, 2)
// x equals 1, and y equals 2

Unlike Objective-C and C, Swift's assignment symbols do not return values ​​themselves. The following statement is illegal:

if x = y {
    // This is illegal, because x = y does not return any value.}

This feature avoids the assignment symbol (=) being accidentally used on the actual intention of equaling the symbol (==). Swift helps you avoid such errors in your code by making if x = y illegal.

Arithmetic operator

  • Add ( + ),: as an infix, it means adding two numerical values. If it is a string, it means string splicing; as a prefix, it means positive number, and the value remains unchanged
  • Subtraction ( - ): As an infix, it means subtraction between two numerical values; as a prefix, it means negative number
  • take ( * )
  • remove ( / )
  • Take the remainder (%): The remainder operator (a % b) can find out how many multiples of b can be put into a  and return the remaining value (which is what we call the remainder). 9 % 4 = 1, because 4 * 2 + 1 = 9

Combination operators

+= 、 -= 、/= 、*=

var a = 1
a += 2

The expression a += 2 is actually the abbreviation of a = a + 2 . In terms of efficiency, one operator composed of the plus sign and the assignment sign can perform these two operations at the same time.

Comparison operator

Here are the regular comparison operators:

  • Equal ( a == b )
  • Not equal ( a != b )
  • greater than ( a > b )
  • Less than ( a < b )
  • greater than or equal to ( a >= b )
  • Less than or equal to ( a <= b )

It can also be used in the same number of tuples, which compare sizes in order from left to right, one value at a time until two unequal values ​​are found. If all values ​​are equal, then the tuple itself is considered equal.

The Swift standard library contains tuple comparison operators that support only tuples with less than seven elements. To compare tuples with seven or more elements, you must implement the comparison operator yourself.

(1, "zebra") < (2, "apple")   // true because 1 is less than 2
(3, "apple") < (3, "bird")    // true because 3 is equal to 3, and "apple" is less than "bird"
(4, "dog") == (4, "dog")      // true because 4 is equal to 4, and "dog" is equal to "dog"
 

Three-eye operator

The ternary conditional operator is a special operation with three parts, which looks like this: question ? answer1 : answer2  . This is a convenient way to choose one of the two expressions based on whether question is true or false.

The ternary conditional operator provides a very effective abbreviation for deciding which one to choose between two expressions. In short, be careful when using the ternary conditional operator. Its simplicity will cause your code to lose its easy-to-read features when reusing it. Avoid combining multiple ternary conditional operators into a single sentence code.

Merge null operators

Merge null operators (a ?? b) If the option a  has a value, expand it. If there is no value, it is nil , then return the default value b. The expression a must be an optional type. The expression b must be the same as the storage type of a .

The merge null operator is the abbreviation of the following code:

a != nil ? a! : b
 

Interval operator

The closed interval operator (a...b) defines a set of ranges from a  to b  and contains a  and b  . The value of a  cannot be greater than b.

for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}

The half-open interval operator ( a..<b ) defines the interval from a  to b  but does not include b , that is, half-open , because it only contains the starting value but does not contain the end value

Single-sided interval: There is another form to make the interval as far as possible in one direction. For example, an interval containing all elements of an array, from index 2 to the end of the array. In this case, you can omit the value on the side of the interval operator. Because the operator has only one side of the value, this interval is called a one-sided interval. Closed and semi-open intervals have the form of a one-sided interval. For example:

let names = ["Anna", "Alex", "Brian", "Jack"]
 
for name in names[2...] {
    print(name)
}
// Brian
// Jack
  
for name in names[...2] {
    print(name)
}
// Anna
// Alex
// Brian
 
for name in names[..<2] {
    print(name)
}
// Anna
// Alex

Logical operators

  • Logical Non  ( !a ): Inverse
  • Logic and ( a && b ): short circuit and. If the first value is false, the second value will be ignored because it can no longer make the entire expression true. This is called short circuit calculation
  • Logical OR (a || b): Short circuit OR. If the expression on the left side of the logic or operator is true , then the right side will not be considered because it will not affect the result of the entire logical expression.
  • In the Swift language, the logical operators && and || are left-related, which means that expressions combined with multiple logical operators will first calculate the leftmost subexpression.

Summarize

This is the end of this article about iOS Swift logical operators. For more related iOS Swift logical operators, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!