Joseph Campbell once said,
“computers are like Old Testament gods: lots of rules and no mercy.”
1. Arithmetic Operators
산술 연산자 +, -, *, /, %(나머지).
Swift는 #의 배수인지 아닌지 더 쉽게 접근할 수 있다.
let number = 465
let isMultiple = number.isMultiple(of: 7)
2. Operator overloading
Swift는 다른 타입에 대해서도 + 연산을 제공한다.
let fakers = "Fakers gonna "
let action = fakers + "fake"
let firstHalf = ["John", "Paul"]
let secondHalf = ["George", "Ringo"]
let beatles = firstHalf + secondHalf
3. Compound assignment operators
var score = 95
score -= 5
var quote = "The rain in Spain falls mainly on the "
quote += "Spaniards"
4. Comparison operators
5. Conditions
if firstCard + secondCard == 2 {
print("Aces – lucky!")
} else if firstCard + secondCard == 21 {
print("Blackjack!")
} else {
print("Regular cards")
}
6. Combining conditions
7. The ternary operator
let firstCard = 11
let secondCard = 10
print(firstCard == secondCard ? "Cards are the same" : "Cards are different")
if firstCard == secondCard {
print("Cards are the same")
} else {
print("Cards are different")
}
8. Switch statements
switch weather {
case "rain":
print("Bring an umbrella")
case "snow":
print("Wrap up warm")
case "sunny":
print("Wear sunscreen")
fallthrough
default:
print("Enjoy your day!")
}
언제 if 대신에 switch를 써야 할까?
9. Range operators
let names = ["Piper", "Alex", "Suzanne", "Gloria"]
print(names[1...3])
print(names[1...]) // 끝까지
10. Operators and conditions summary
'📕 iOS > 100 Days of Swift' 카테고리의 다른 글
[Day 4] loops, loops, and more loops (0) | 2023.06.21 |
---|---|
[Day 2] arrays, dictionaries, sets, and enums (0) | 2023.04.06 |
[Day 1] variables, simple data types, and string interpolation (0) | 2023.04.05 |
[Day 0] 100 Days of Swift 시작하기 (0) | 2023.04.04 |