Basic Operators of Swift

227 阅读2分钟

assignment operator

  • it the right side of a assignment operator is a tuple with multiple values, you can decompose its elements into multiple constants or variables
let (x, y) = (1, 2)
  • the assignment operator does not return a value

arithmetic operators

  • 4 standerd arithmetic operators for all number type
    • addition (+)
    • subtraction (-)
    • multiplication(*)
    • division(/)
  • arithmetic operators don's allow values to overflow by default
  • addition operator is supported for String concatenation
let helloworld = "hello " + "world!"

remainder operator

let a = 5, b = -3
let c = a % b // c = 2

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

compound assignment operators

  • compound assignment operators combine assignment with other operator
var a = 1
a += 1

the compound operators don't return a value. you can't write let b = a += 2

comparison operators

  • identity operators === and !==
  • you can use identity operators to find out whether 2 object references both refer to the same object
  • you can compare 2 tuples if they have the same type and the same number of values
  • tuples compare from left to right, one value at a time, until the comparison finds 2 values that are not equal
(1, "z") < (2, "a")
(3, "a") < (3, "b")
(4, "d") == (4, "d")
  • tuples can be compared with a given operator only if the operator can be applied to each value in the tuples
("blue", -1) < ("purple", 1)        // OK, evaluates to true
("blue", false) < ("purple", true)  
// Error because < can not compare Boolean values

nil-coalescing operator

  • the nil-coalescing operator (a ?? b)unwrap an optional a if a contains a value, or return b if a is nil
  • a is always of an optional type, and b must match the type that is stored inside a
  • the nil-coalescing operator can be converted to ternary conditional operator
a ?? b
a != nil ? a! : b

note: if a is non-nil, the value of b is not evaluated

let defaultColorName = "red"
var userDefineColorName: String?
var colorName = userDefineColorName ?? defaultColorName

range operators

closed range operator

  • the closed range operator (a...b) defines a range from a to b, and include a and b

half-open range operator

  • hte half-open range operator (a..<b) defines a range from a to b, includes a but doesn't include b
  • if a equals to b, the resulting range will be empty
  • half-open range operator is 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) is \(names[i])")
}

one-sided ranges

  • the range continues as far as possible in one direction
for name in names[...2] {
    print(name)
}
//Anna Alex Brian
for name in names[2..] {
    print(name)
}
//Brian Jack
  • the half-open range alse has a one-sided form that is written with only its final value, and the final value is not in the range
for name in names[..<2] {
   print(name) 
}
//Anna Alex
  • you can check whether a one-sided range contains a particular value
let range = ...10
range.contains(11) //false
range.contains(0) //true
range.contains(-11) // true

logical operators

  • there are 3 standerd logical operators (!, &&, ||)
if enteredDoorCode && passedRedinaScan || hasDoorKey || knowOverridePasswrod {
    print("Welcome")
} else {
    print("access denied")
}
if (enteredDorrCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword {
    print("welcome")
} else {
    print("access denied")
}