Assignment Operator

let b = 10
var a = 5
a = b
// a is now equal to 10

let (x, y) = (1, 2)
// x is equal to 1, and y is equal to 2

the assignment operator in Swift does not itself return a value. The following statement is not valid.

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

Arithmetic Operators

Swift supports the four standard arithmetic operators for all number types:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)

1 + 2       // equals 3
5 – 3       // equals 2
2 * 3       // equals 6
10.0 / 2.5  // equals 4.0

the Swift arithmetic operators don’t allow values to overflow by default. You can opt in to value overflow behavior by using Swift’s overflow operators (such as a &+ b)

The addition operator is also supported for String concatenation:

“hello, ” + “world”  // equals “hello, world”

Remainder Operator

The remainder operator (a % b)

9 % 4    // equals 1

-9 % 4   // equals -1

The sign of b is ignored for negative values of b. This means that a % b and a % -b always give the same answer.

Unary Minus Operator

let three = 3
let minusThree = -three       // minusThree equals -3
let plusThree = -minusThree   // plusThree equals 3, or “minus minus three”

Unary Plus Operator

let minusSix = -6
let alsoMinusSix = +minusSix  // alsoMinusSix equals -6

the unary plus operator doesn’t actually do anything.

Compound Assignment Operators

var a = 1
a += 2
// a is now equal to 3

NOTE

The compound assignment operators don’t return a value. For example, you can’t write let b = a += 2.

Comparison Operators

  • Equal to (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)

NOTE

Swift also provides two identity operators (=== and !==), which you use to test whether two object references both refer to the same object instance.

Each of the comparison operators returns a Bool value to indicate whether or not the statement is true:

1 == 1   // true because 1 is equal to 1

You can compare two tuples if they have the same type and the same number of values. Tuples are compared from left to right, one value at a time, until the comparison finds two values that aren’t equal.

(1, “zebra”) < (2, “apple”)   // true because 1 is less than 2; “zebra” and “apple” are not compared
(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”

In the example above, you can see the left-to-right comparison behavior on the first line. Because 1 is less than 2, (1, "zebra") is considered less than (2, "apple"), regardless of any other values in the tuples. It doesn’t matter that "zebra" isn’t less than "apple", because the comparison is already determined by the tuples’ first elements. However, when the tuples’ first elements are the same, their second elements arecompared—this is what happens on the second and third line.

Tuples can be compared with a given operator only if the operator can be applied to each value in the respective tuples. For example, as demonstrated in the code below, you can compare two tuples of type (String, Int) because both String and Int values can be compared using the < operator. In contrast, two tuples of type (String, Bool) can’t be compared with the < operator because the < operator can’t be applied to Bool values.

(“blue”, -1) < (“purple”, 1)        // OK, evaluates to true
(“blue”, false) < (“purple”, true)  // Error because < can’t compare Boolean values

NOTE

The Swift standard library includes tuple comparison operators for tuples with fewer than seven elements. To compare tuples with seven or more elements, you must implement the comparison operators yourself.

Ternary Conditional Operator

question ? answer1 : answer2

와 아래는 같은내용

if question {
   answer1
} else {
   answer2
}

Nil-Coalescing Operator

The nil-coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil. The expression a is always of an optional type. The expression b must match the type that is stored inside a.

a ?? b  는 아래돠 같은 내용

a != nil ? a! : b

NOTE

If the value of a is non-nil, the value of b is not evaluated. This is known as short-circuit evaluation.

Range Operators

closed range operator

a...b defines a range that runs from a to b, and includes the values a and b

for index in 1…5 {
   print(“(index) times 5 is (index * 5)”)
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25

Half-Open Range Operator

a..<b defines a range that runs from a to b, but doesn’t include b

Half-open ranges are particularly useful when you work with zero-based lists such as arrays. 

let names = [“Anna”, “Alex”, “Brian”, “Jack”]
let count = names.count
for i in 0..<count {
   print(“Person (i + 1) is called (names[i])”)
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack

One-Sided Ranges

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

let range = …5
range.contains(7)   // false
range.contains(4)   // true
range.contains(-1)  // true

Logical Operators

  • Logical NOT (!a)
  • Logical AND (a && b)
  • Logical OR (a || b)

Logical NOT Operator

The logical NOT operator (!a) inverts a Boolean value so that true becomes false, and false becomes true.

Logical AND Operator

let enteredDoorCode = true
let passedRetinaScan = false
if enteredDoorCode && passedRetinaScan {
   print(“Welcome!”)
} else {
   print(“ACCESS DENIED”)
}
// Prints “ACCESS DENIED”

Logical OR Operator

let hasDoorKey = false
let knowsOverridePassword = true
if hasDoorKey || knowsOverridePassword {
   print(“Welcome!”)
} else {
   print(“ACCESS DENIED”)
}
// Prints “Welcome!”

Comments are closed.

Post Navigation