let a = 123
let b = 456
let c: Int? = 789
-
Unary Operator (λ¨ν μ°μ°μ) : -a
- Prefix (μ μ νκΈ°λ²) : -a
- Postfix (νμ νκΈ°λ²) : c!
-
Binary Operator (μ΄ν μ°μ°μ) : a + b
-
Infix (μ€μ νκΈ°λ²) : a + b
-
Ternary Operator (μΌν μ°μ°μ)
(Swift μμ μΌν μ°μ°μλ λ¨ νλ)
a > 0 ? "positive" : "zero or negative"
if a > 0 {
"positive"
} else {
"negative"
}
var value = 0
value = value + 10
value = value - 5
value = value * 2
value = value / 2
value = value % 2
10 % 2 // λλ¨Έμ§ 0
11 % 2 // λλ¨Έμ§ 1
13 % 3 // λλ¨Έμ§ 1
// Compound assignment Operators
```swift
value += 10 //λν κ°μ΄ 10κ³Ό κ°μ λ
value -= 5 //λΊ κ°μ΄ 5μ κ°μ λ
value *= 2 //κ³±ν κ°μ΄ 2μ κ°μ λ
value /= 2 //λλ κ°μ΄ 2μ κ°μ λ
value %= 2 //λλ¨Έμ§κ° 2μ λμΌ
// λ―Έμ§μ : value++ , value--
// value ++
// value += 1
// value = value + 1
var (x, y) = (1, 2)
print(x, y)
μ°μ°μ μ°μ μμ
1 + 2 * 3
1 + (2 * 3)
(1 + 2) * 3
1 + 2 * 3 + 3
1 + (2 * 3) + 3
1 + 2 * (3 + 3)
write | mean |
---|---|
a == b | Equal to operator |
a != b | Not equal to operator |
a > b | Greater than operator |
a >= b | Greater than or equal to operator |
a < b | Less than operator |
a <= b | Less than or equal to operator |
μΆμ : https://ndb796.tistory.com/3
- "iOS" > "Apple" = true (μμ€ν€, μ λμ½λλ‘ κ³μ°, iκ° λνΌ)
- "Application" > "Steve Jobs" = false (Aκ° λ νΌ)
- "Swift5" <= "Swift4" = false (SwftκΉμ§ λͺ¨λ λμΌνλ€κ° λ§μ§λ§ μμμ κ°λ¦Ό)
- "Playground" == "Playground" true
// Logical AND Operator
true && true //t
true && false //f
false && true //f
false && false //f
// Logical OR Operator
true || true //t
true || false //t
false || true //t
false || false //f
// Logical Negation Operator
!true //f
!false //t
// Combining Logical Operators
let enteredDoorCode = true //t
let passedRetinaScan = false //f
let hasDoorKey = false //f
let knowsOverridePassword = true //t
// λ€μ λ
Όλ¦¬μμμ μ΄λ€ μν©μΌ λ "Open the door"κ° μΆλ ₯λ κΉμ?
if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
print("Open the door")
} else {
print("Can't open the door")
}
// Explicit Parentheses
if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword {
// ...
} else {
// ...
}
- μμλ₯Ό ν΅ν΄ μλμμλ₯Ό μμλ΄ μλ€.
- μ°μ μμ : && > ||
func returnTrue() -> Bool {
print("function called")
return true
}
// μλ 3κ°μ μΌμ΄μ€μμ returnTrue λ©μλλ κ°κ° λͺ λ²μ© νΈμΆλ κΉ?
print("\n---------- [ Case 1 ] ----------\n")
returnTrue() && returnTrue() && false || true && returnTrue()
//3λ².------------- λκ°λ¨Όμ κ³μ°λμ΄ νμ€, λ€μ νμ€ μ°Έ, λ€μ or κ·Έ μ μ μ°Έ||μ°Έ = μ°Έ
print("\n---------- [ Case 2 ] ----------\n")
returnTrue() && false && returnTrue() || returnTrue()
//2λ². ------------------------------3κ° λ¨Όμ νΈλ£¨λ‘ κ³μ°, κ²°κ³Όμ μΌλ‘ μ°Έ||μ°Έ = μ°Έ
print("\n---------- [ Case 3 ] ----------\n")
returnTrue() || returnTrue() && returnTrue() || false && returnTrue()
//3λ².---------------------------------------맨 μμ μ°Έμ΄λ©΄ λ€μ ||λ€μ $$κ°μλ μ°Έ μ΄ν &&μμ μ°Έκ°λ¦Ό.
// (1+2+3)*4 = μ΄λ°μμΌλ‘.
print("\n---------- [ Range Operators ] ----------\n")
// Closed Range Operator
0...100
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
// Half-Open Range Operator
0..<100
let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count { // 0, 1, 2, 3
print("Person \(i + 1) is called \(names[i])")
}
// One-Sided Ranges
1...
...100
..<100
// 0 1 2 3
//let names = ["Anna", "Alex", "Brian", "Jack"]
names[2...] //2μ΄μ
names[...2] //2μ΄ν
names[..<2] //2λ³΄λ€ μμ μΈλ±μ€
// μμλ₯Ό λ°λλ‘ μ μ©νλ λ°©λ²
for index in (1...5) {
print("\(index) times 5 is \(index * 5)")
}
// 1) reversed
for index in (1...5).reversed() {
print("\(index) times 5 is \(index * 5)")
}
print()
// 2) stride
for index in stride(from: 5, through: 1, by: -1) {
print("\(index) times 5 is \(index * 5)")
}
print()
- reversed κ°μ λ©μλλ₯Ό μ°μ§ μκ³ μμμΌλ‘ μΆλ ₯λκ² νκΈ°
let range = 1...5
range.lowerBound // ννμ
range.upperBound // μνμ
for index in range {
// 1 : 5 - 1 + 1 = 5
// 2 : 5 - 2 + 1 = 4
// ...
let num = range.upperBound - index + range.lowerBound
print("\(num) times 5 is \(num * 5)")
}