- if
- swhich
- guard
if์ ๊ธฐ๋ณธํ
- if๋ ๋ฐ๋์ Bool ๊ฐ (true, false)์ ์ฌ์ฉํ๋ค.
์คํ๊ตฌ๋ฌธ
}
if ์กฐ๊ฑด {
์คํ๊ตฌ๋ฌธ
} else {
์คํ๊ตฌ๋ฌธ
}
// if
var temperatureInFahrenheit = 30
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
}
// if - else
temperatureInFahrenheit = 40
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
} else {
print("It's not that cold. Wear a t-shirt.")
}
// if - else if - else
temperatureInFahrenheit = 90
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
print("It's really warm. Don't forget to wear sunscreen.")
} else {
print("It's not that cold. Wear a t-shirt.")
}
// if - else if
temperatureInFahrenheit = 72
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
print("It's really warm. Don't forget to wear sunscreen.")
}
var number = 9
if number < 10 {
print("10๋ณด๋ค ์๋ค")
} else if number < 20 {
print("20๋ณด๋ค ์๋ค")
}
// ์์ ๊ฒ๋ง ์คํ๋จ(์ ํ)
if number < 10 {
print("10๋ณด๋ค ์๋ค")
}
if number < 20 {
print("20๋ณด๋ค ์๋ค")
}
//๋๊ฐ ๊ตฌ๋ถ์ง์ด ๋์ผ๋ฉด ๋ ๋ค ์คํ๋จ
switch ๊ธฐ๋ณธํ
- switch ๋ฌธ์ ๊ฐ๋ฅํ ๋ชจ๋ ์ฌ๋ก๋ฅผ ๋ฐ๋์ ๋ค๋ฃจ์ด์ผ ํ๋ค. (Switch must be exhaustive)
switch ๊ฐ {
case ๋น๊ต๊ฐ 1:
์คํ๊ตฌ๋ฌธ
case ๋น๊ต๊ฐ 2:
์คํ๊ตฌ๋ฌธ
fallthrough // ๋ค์ case๊น์ง๋ง ๋ง์น๊ณ ์ข
๋ฃ.
case ๋น๊ต๊ฐ1, ๋น๊ต๊ฐ2, ๋น๊ต๊ฐ3 : //๋ค์ค์ ๊ฒฝ์ฐ
์คํ๊ตฌ๋ฌธ
default: //ํด๋น๋์ง ์๋ ๊ฐ ๋ฐํ(else)
default์ ์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ
let isTrue = true
type(of: isTrue)
switch isTrue {
case true:
print("true")
case false:
print("false")
}
๋ฒ์์ ๋งค์นญ
let approximateCount = 30
switch approximateCount {
case 0...50:
print("0 ~ 50")
case 51...100:
print("51 ~ 100")
default:
print("Something else")
}```
```swift
switch temperatureInFahrenheit {
case 0...32 :
print("It's very cold. Consider wearing a scarf.")
case 86... :
print("It's really warm. Don't forget to wear sunscreen.")
default:
print("It's not that cold. Wear a t-shirt.")
}
- if๋ฌธ์ ์ฝค๋ง(,)๊ฐ And(&&)์ฐ์ฐ์ธ ๊ฒ๊ณผ ๋ฐ๋๋ก switch๋ฌธ์ ์ฝค๋ง(,)๊ฐ Or(||) ์ฐ์ฐ
let someCharacter: Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u": //์ด๊ฑฐ๋(OR)
print("\(someCharacter) is a vowel")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
print("\(someCharacter) is a consonant")
default:
print("\(someCharacter) is not a vowel or a consonant")
}
x, y ์ขํ(x์ถ, y์ถ ํ์ฉ)
let somePoint = (9, 0)
switch somePoint {
case (let distance, 0), (0, let distance): //x์ขํ๊ฐ 0์ด๊ฑฐ๋ y์ขํ๊ฐ 0
print("On an axis, \(distance) from the origin") //distance๋งํผ ๋จ์ด์ง ์ขํ์ ์๋ค.
default:
print("Not on an axis") //์ถ์ ํด๋นํ์ง ์๋๋ค
}
### where clause
```swift
let anotherPoint = (1, -1)
switch anotherPoint {
case let (x, y) where x == y: //x๊ฐ y๋ ๊ฐ์๋
//(let x, let y, let z)์ ๋์ผํ ์๋ฏธ.
print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y: // x๊ฐ -y์ ๊ฐ์๋
print("(\(x), \(y)) is on the line x == -y")
case let (x, y)://ํด๋น์์
print("(\(x), \(y)) is just some arbitrary point")
}
- ์ด๋ค ์ซ์๊ฐ ์ฃผ์ด์ก์ ๋ ์ง์์ธ์ง ํ์์ธ์ง ์ถ๋ ฅ (switch์ where ์ ์ด์ฉ)
let even = 4
switch even {
case let x where x.isMultiple(of: 2):
//2 == 0 ์ ๋์ผํ ์๋ฏธ
print("\(x)๋ ์ง์")
default:
print("ํ์")
}
- fallthrough ์๋๋ฐฉ์
let integerToDescribe = 3
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and also" //
// fallthrough
default:
description += " an integer."
}
print(description)
//fallrhrough ๋ฅผ ์ถ๊ฐํ๋ฉด ํ๋จ๊ณ ์๋๊น์ง๋ง ์คํ๋จ
- _if_๋ฌธ์ ๊ฒฝ์ฐ ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด ํด๋น ๋ถ๊ธฐ๋ฌธ์ ์คํํ์์ผ๋, _guard_๊ตฌ๋ฌธ์ ๊ฒฝ์ฐ ์กฐ๊ฑด์ ๋ง์ง ์์ผ๋ฉด ๋ฐ๋ก ์ข ๋ฃ๋จ.
func someFunction() {
// ...
// ...
// if ๋ฌธ์ ์กฐ๊ฑด์ด ๋ง์ผ๋ฉด ์ด๋ค ์ฝ๋๋ฅผ ์ํํ ๊ฒ
if Bool.random() { //๋ถ๊ฐ์ค. ์ฐธ,๊ฑฐ์ง์ค ํ๋๋ฉด,
// ...
}
//print(1) ์ถ๋ ฅ
// gaurd๋ฌธ์ ์กฐ๊ฑด์ ๋ง์กฑํ ๋๋ง ๋ค์์ผ๋ก ๋์ด๊ฐ ๊ฒ
guard Bool.random() else { return }
//print(1) //๊ฐ๋๋ฌธ์ ๊ฒฝ์ฐ , ํธ๋ฃจ๋ฉด ์ถ๋ ฅ , ํ์ค๋ฉด ์ถ๋ ฅ ์๋จ
- ๊ฐ๋๋ฌธ์ ๋ค์ฌ์ฐ๊ธฐ๋ฅผ ํ์ง ์๋๋ค. (์ผ๋ ฌ๋ก ์ญ ๋์ด)
guard๋ฌธ ์์
func update(age: Int) {
guard 1...100 ~= age else { return }//์ข
๋ฃ์ฝ๋
print("Update")
}
update(age: -1)
update(age: 2)
update(age: 100)
let number: Int = 10
let threeNumbers: (Int, Int, Int) = (1, 2, 5)
type(of: threeNumbers)
threeNumbers
threeNumbers.0//1
threeNumbers.1//2
threeNumbers.2//5
var threeValues: (Int, Double, String) = (10, 100.0, "์ด๋ฆ")
type(of: threeValues)//ํ์
์ถ์ถ
threeValues
threeValues.0 = 5
threeValues
-
Tuples์ ์ฅ์
- ์์์ ์ด๋ฆ๊ณผ ์์ ํ์ฉ
- ํ์ ํผํฉํ์ฌ ์ด์ฉ๊ฐ๋ฅ
let numbers = threeNumbers
numbers
numbers.0 //1
numbers.1 //2
let (first, second, third) = threeNumbers
first//1
second//2
third//3
// wildcard pattern
let (_, second1, third1) = threeNumbers //_์์ผ๋์นด๋ํจํด : ์ฒซ๋ฒ์งธ ๊ฒ์ ๋ฐ๊ณ ์ถ์ง. ์๊ณ ์๋ตํ๊ณ ์ถ๋ค.
second1 //2
third1 //5
๊ธฐ๋ณธ๊ตฌ์กฐ
let iOS: (language: String, version: String) = (language: "Swift", version: "5")
let iOS: (language: String, version: String) = ("Swift", "5")
let iOS = (language: "Swift", version: "5")
ํ์ฉ
let iOS = (language: "Swift", version: "5")
iOS.0//swift
iOS.1//5
iOS.language
iOS.version
func ํํ(a: Int, b: (Int, Int)) -> (first: Int, second: Int) {
return (a + b.0, a + b.1)
}//10+20 , 10+30
let result = ํํ(a: 10, b: (20, 30))
result.first //30
result.second //40
result.0 //30
result.1 //40
Tuple ์ 7๊ฐ ๋ฏธ๋ง ์์์ ๋ํ ๋น๊ต ์ฐ์ฐ์๊ฐ ํฌํจ๋์ด ์์(6๊ฐ๊น์ง๋ง ์ธ ์ ์์).7๊ฐ ์ด์์ ์์๋ฅผ ๋น๊ตํ๊ธฐ ์ํด์๋ ๋น๊ต ์ฐ์ฐ์๋ฅผ ์ง์ ๊ตฌํํด์ผ ํจ
var something1: (Int, Int, Int, Int, Int, Int) = (1,2,3,4,5,6)
var something2: (Int, Int, Int, Int, Int, Int) = (1,2,3,4,5,6)
something1 == something2
- ํํ ๋น๊ต ์ฐ์ฐ์ ๋ํ ๊ฒฐ๊ณผ๋?
(1, "zebra") < (2, "apple") //true
(3, "apple") > (3, "bird") //false ์์คํค
("blue", 1) > ("bluesky", -1) //false
("์ผ", 1) > ("์ด", 2.0) //true //ํ๊ธ : ์ด์ฑ,์ค์ฑ,์ข
์ฑ ใ
+ใ
ฃ+ใน > ใ
+ใ
ฃ
//(1, "zebra") < ("2", "apple")// ๋น๊ต๋ถ๊ฐ -> ํ์
์ด ๋ฌ๋ผ์ error
//("blue", false) < ("purple", true)// ํธ๋ฃจ,ํ์ค๋ ๋์๋น๊ต๋ถ๊ฐ error
let somePoint = (1, 0)
switch somePoint {
case (0, 0):
print("\(somePoint) is at the origin") //x, y๊ฐ 0์ผ ๋
case (_, 0):
print("\(somePoint) is on the x-axis") //y๊ฐ 0์ผ ๋
case (0, _):
print("\(somePoint) is on the y-axis") //x๊ฐ 0์ผ ๋
case (-2...2, -2...2):
print("\(somePoint) is inside the box")//๊ฐ๊ฐ์ ๊ฐ ๋ชจ๋ ์ถฉ์กฑ
default:
print("\(somePoint) is outside of the box") //์ธ
}
// ๋งจ ์๋ถํฐ ๊ฒ์ฌํ๋ฏ๋ก ์์์ค์
๊ธฐ๋ณธํ
let ํจ์ = ["ํค":"๊ฐ"] ```
```swift
let fruits = ["A": "Apple", "B": "Banana", "C": "Cherry"]
for (key, value) in fruits {
print(key, value)
}
print()
for element in fruits {
// ์ด๋ค ์์ผ๋ก ์ถ๋ ฅํด์ผ ํ ๊น์?
//print(element.0,element.1) //ํํ๋๋ฒ ์ด์ฉ
//prein(element.key, element.value) //ํํ๋ค์ ์ด์ฉ
}