Sequence 를 iterate 할 때 for-in loop 사용
Dictionary 의 경우 순서가 없기 때문에 iteration 은 순서를 보장하지 않음. dictionary 에 item 을 집어넣는 순서도 순서에 영향을 주지 않음
// array
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
print("Hello, \(name)!")
}
// dictionary
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
print("\(animalName)s have \(legCount) legs")
}
// range
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
for index in 1...5 에서 index 는 loop 의 각 iteration 시작마다 자동으로 설정되는 상수. 따라서 사용 전에 미리 정의될 필요없음. 각 값이 필요없다면 underscore _ 로 변수 이름을 대체할 수 있음
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
}
print("\(base) to the power of \(power) is \(answer)")
// Prints "3 to the power of 10 is 59049".
stride(from:to:by:) 메서드를 통해 필요하지 않은 mark 를 제외할 수 있음. Closed range 는 stride(from:through:by:) 를 사용
let minuteInterval = 5
for tickMark in stride(from: 0, to: 60, by: minuteInterval) {
// render the tick mark every 5 minutes (0, 5, 10, 15 ... 45, 50, 55)
}
let hours = 12
let hourInterval = 3
for tickMark in stride(from: 3, through: hours, by: hourInterval) {
// render the tick mark every 3 hours (3, 6, 9, 12)
}
조건이 false 가 될 때까지 loop 를 수행함. 첫 번째 iteration 수행 전에 얼마나 많은 iteration 이 필요한지 알려지지 않았을 때 유용.
while : 각 loop 의 시작에 조건을 따짐repeat-while : 각 loop 의 마지막에 조건을 따짐while <#condition#> {
<#statements#>
}
var square = 0
var diceRoll = 0
while square < finalSquare {
// roll the dice
diceRoll += 1
if diceRoll == 7 { diceRoll = 1 }
// move by the rolled amount
square += diceRoll
if square < board.count {
// if we're still on the board, move up or down for a snake or a ladder
square += board[square]
}
}
print("Game over!")
loop block 을 먼저 수행하고 조건을 따짐
repeat {
<#statements#>
} while <#condition#>
repeat {
// move up or down for a snake or ladder
square += board[square]
// roll the dice
diceRoll += 1
if diceRoll == 7 { diceRoll = 1 }
// move by the rolled amount
square += diceRoll
} while square < finalSquare
print("Game over!")
조건이 true 일 때 실행
if-else 에서 else 부분은 조건이 flase 일 때 실행. 여러 if 문을 엮을 수 있음
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 { //optional. 없어도 됨
print("It's not that cold. Wear a T-shirt.")
}
// Prints "It's really warm. Don't forget to wear sunscreen."
// 각 branch 는 하나의 값을 갖고 있음. branch 가 true 인 경우 그 값을 변수에 할당함
// 이 경우 항상 else 까지 있어서 최소한 하나라도 매칭이되도록 함
let weatherAdvice = if temperatureInCelsius <= 0 {
"It's very cold. Consider wearing a scarf."
} else if temperatureInCelsius >= 30 {
"It's really warm. Don't forget to wear sunscreen."
} else {
"It's not that cold. Wear a T-shirt."
}
print(weatherAdvice)
// Prints "It's not that cold. Wear a T-shirt."
// 아래의 경우 nil을 리턴하는데 타입을 명시적으로 적어줘야 함
let freezeWarning: String? = if temperatureInCelsius <= 0 {
"It's below freezing. Watch for ice!"
} else {
nil // 아니면 nil as String? 과 같이 쓰고 타입 명시를 안해줘도 됨
}
아래와 같이 error 를 throw 하거나 절대로 return 하지 않는 fatalError(_:file:line:) 같은 메서드를 호출할 수 있음
// try 를 붙이지 않는 것에 유의
let weatherAdvice = if temperatureInCelsius > 100 {
throw TemperatureError.boiling
} else {
"It's a reasonable temperature."
}
값을 보고 가능한 matching pattern 과 비교함. 첫 번째로 매칭된 패턴에 있는 코드를 실행함.
모든 swtich 문은 exhaustive 이어야 함. 즉 가능한 모든 값들이 최소 하나의 switch case 에 매칭되어야 함. 모든 case 를 명시하는 게 적합하지 않은 경우 default 를 통해 명시되지 않은 값들을 커버. default 는 항상 맨 마지막에 나와야 함
switch <#some value to consider#> {
case <#value 1#>:
<#respond to value 1#>
case <#value 2#>,
<#value 3#>:
<#respond to value 2 or 3#>
default:
<#otherwise, do something else#>
}
// switch 문은 항상 exhaustive 하기 때문에 할당할 값이 존재함
let anotherCharacter: Character = "a"
let message = switch anotherCharacter {
case "a":
"The first letter of the Latin alphabet"
case "z":
"The last letter of the Latin alphabet"
default:
"Some other character"
}
print(message)
// Prints "The first letter of the Latin alphabet".
C, Obj-C 와는 다르게 Swift 의 switch 문은 각 case 끝에서 fall through 를 하고 다음 case 로 넘어가지 않음. 대신 처음으로 매칭된 case 문이 완료되면 명시적인 break 문 없이 전체 switch 문이 끝나게 됨.
이는 switch 문을 안전하고 쉽게 사용할 수 있게 해줌
각 case 문은 무조건 최소 한 줄의 실행 가능한 코드가 있어야 함. 가독성을 위해 case 를 여러 줄에 걸쳐 쓸 수도 있긴 함
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a": // ❗️ Invalid, the case has an empty body. Compile time error 를 통해 실수로 fallthrough 를 하는 것 방지
case "A":
print("The letter A")
default:
print("Not the letter A")
}
// This will report a compile-time error.
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a", "A": // 가능
print("The letter A")
default:
print("Not the letter A")
}
// Prints "The letter A".
switch case 는 특정 범위 내 포함 여부도 확인 가능
let approximateCount = 62
let countedThings = "moons orbiting Saturn"
let naturalCount: String
switch approximateCount {
case 0:
naturalCount = "no"
case 1..<5:
naturalCount = "a few"
case 5..<12:
naturalCount = "several"
case 12..<100:
naturalCount = "dozens of"
case 100..<1000:
naturalCount = "hundreds of"
default:
naturalCount = "many"
}
print("There are \(naturalCount) \(countedThings).")
// Prints "There are dozens of moons orbiting Saturn."
tuple 을 사용해서 여러 값을 매칭할 수 있음. Wildcard pattern _ 을 사용해서 가능한 모든 값을 매칭할 때 사용
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
print("\(somePoint) is at the origin")
case (_, 0):
print("\(somePoint) is on the x-axis")
case (0, _):
print("\(somePoint) is on the y-axis")
case (-2...2, -2...2):
print("\(somePoint) is inside the box")
default:
print("\(somePoint) is outside of the box")
}
// Prints "(1, 1) is inside the box".
case 문에서 특정 값을 임시 상수/ 변수에 매칭시킬 수 있음.
Value binding : 값이 case 문에서 임시 상수/변수에 bound 됨
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0): // y값이 0이고, x부분에 해당하는 값을 임시 상수 x 에 할당
print("on the x-axis with an x value of \(x)")
case (0, let y):
print("on the y-axis with a y value of \(y)")
case let (x, y): // 여기에서 모든 값을 커버하기 때문에 default 는 필요가 없고 exhaustive 하게 매칭됨
print("somewhere else at (\(x), \(y))")
}
// Prints "on the x-axis with an x value of 2".
where 을 사용해서 추가 조건을 확인할 수 있음
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
print("(\(x), \(y)) is just some arbitrary point")
}
// Prints "(1, -1) is on the line x == -y".
여러 패턴을 여러 줄에 걸쳐 쓸 수 있음. Value ㅠㅑㅜ야ㅜㅎ eh vhgka rksmd
let someCharacter: Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
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) isn't a vowel or a consonant")
}
// Prints "e is a vowel".
let stillAnotherPoint = (9, 0)
switch stillAnotherPoint {
case (let distance, 0), (0, let distance): // distance 로 통일해서 case body 문에서 항상 distance 에 접근 가능하도록 해야 함
print("On an axis, \(distance) from the origin")
default:
print("Not on an axis")
}
// Prints "On an axis, 9 from the origin".
switch 예시들에서 각 switch case 는 특정 case 에 매칭하는 value 의 패턴을 포함함. if 문에서도 pattern 을 조건으로 사용할 수 있음
let somePoint = (12, 100)
// 조건이 pattern. pattern 이 matching 되면 if 는 true 인 것으로 판단됨
// switch case 에 쓰는 패턴과 같음
if case (let x, 100) = somePoint {
print("Found a point on the y=100 line, at \(x)")
}
// Prints "Found a point on the y=100 line, at 12".
for-in 문에서는 case 없이도 value binding 을 사용할 수 있음
let points = [(10, 0), (30, -30), (-20, 0)]
for (x, y) in points {
if y == 0 {
print("Found a point on the x-axis at \(x)")
}
}
// Prints "Found a point on the x-axis at 10".
// Prints "Found a point on the x-axis at -20".
// 위와 같은 코드. 더 간결하게 작성함
for case (let x, 0) in points {
print("Found a point on the x-axis at \(x)")
}
// Prints "Found a point on the x-axis at 10".
// Prints "Found a point on the x-axis at -20".
for-case-in 은 where 을 포함할 수 있음
for case let (x, y) in points where x == y || x == -y {
print("Found (\(x), \(y)) along a line through the origin")
}
// Prints "Found (30, -30) along a line through the origin".
// 얘도 가능
for (x, y) in [(1, 2)] where x == y || x == -y {
print("Found (\(x), \(y)) along a line through the origin")
}
Pattern 은 value 을 binding 할 수 있기 때문에 if-case 문과 for-case-in loop 는 associated value 를 가진 enum 에 유용함
제어를 한 코드에서 다른 코드로 이동시켜서 코드가 실행되는 순서롤 바꿈.
continuebreakfallthroughreturnthrowLoop 를 멈추고 다음 iteration 의 시작으로 가게 함. ‘현재 loop iteration 을 끝내면서’ loop 전체를 끝내지는 않음
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
let charactersToRemove: [Character] = ["a", "e", "i", "o", "u", " "]
for character in puzzleInput {
if charactersToRemove.contains(character) {
continue
}
puzzleOutput.append(character)
}
print(puzzleOutput)
// Prints "grtmndsthnklk".
전체 control flow 를 즉시 중단시킴. switch / loop 안에서 사용해서 원래 끝내야 하는 것보다 빨리 실행을 중단시키고 싶을 때 사용 가능
Loop 의 실행을 중단하고 loop 다음에 작성된 코드로 이동. 현재 iteration 이 중단될 뿐만 아니라 이후의 iteration 도 시작되지 않음
switch 문을 즉시 중단하고 switch 문 다음 코드를 실행함. 특정 case 를 무시하고 싶을 때 사용할 수 있음
let numberSymbol: Character = "三" // Chinese symbol for the number 3
var possibleIntegerValue: Int?
switch numberSymbol {
case "1", "١", "一", "๑":
possibleIntegerValue = 1
case "2", "٢", "二", "๒":
possibleIntegerValue = 2
case "3", "٣", "三", "๓":
possibleIntegerValue = 3
case "4", "٤", "四", "๔":
possibleIntegerValue = 4
default:
break
}
if let integerValue = possibleIntegerValue {
print("The integer value of \(numberSymbol) is \(integerValue).")
} else {
print("An integer value couldn't be found for \(numberSymbol).")
}
// Prints "The integer value of 三 is 3."
Swift 에서 switch 문은 자동으로 fall through 되어 각 case 에서 다음 case 로 넘어가지 않음. Fallthrough 를 막아둠으로써 C보다 정화하고 예상가능하도록 동작하게 함.
fallthrough 키워드를 통해 C 스타일대로 작성할 수 있음. Fallthrough 는 다음 case 의 조건을 확인하지 않고 바로 실행함.
let integerToDescribe = 5
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)
// Prints "The number 5 is a prime number, and also an integer."
Swift 에서는 Loop 를 중첩시킬 수 있음. 각자의 loop 가 자신만의 break 를 사용해서 실행을 중단시킬 수 있기 때문에 어떤 loop 을 break/continue 할 것인지를 명시적으로 표현하는 것이 유용함.
Statement label 을 사용해서 loop / 조건문을 표시할 수 있음.
break 문을 통해 label 된 조건문을 종료시킬 수 있음break / continue 를 통해 label 된 loop 를 종료시킬 수 있음// while loop 의 statement label 예시
<#label name#>: while <#condition#> {
<#statements#>
}
gameLoop: while square != finalSquare {
diceRoll += 1
if diceRoll == 7 { diceRoll = 1 }
switch square + diceRoll {
case finalSquare:
// diceRoll will move us to the final square, so the game is over
break gameLoop
case let newSquare where newSquare > finalSquare:
// diceRoll will move us beyond the final square, so roll again
continue gameLoop
default:
// this is a valid move, so find out its effect
square += diceRoll
square += board[square]
}
}
print("Game over!")
guard 문은 조건에 따라 코드를 실행시킴. guard 문 내부에 있는 조건이 true 여야 guard 문 다음의 코드가 실행됨. False 인 경우 else 클로저 내부의 코드가 실행됨
guard 문은 코드의 가독성을 향상시킴
func greet(person: [String: String]) {
guard let name = person["name"] else {
return // return, break, continue, throw, fatalError 같은 코드 실행 가능
}
print("Hello \(name)!")
guard let location = person["location"] else {
print("I hope the weather is nice near you.")
return
}
print("I hope the weather is nice in \(location).")
}
greet(person: ["name": "John"])
// Prints "Hello John!"
// Prints "I hope the weather is nice near you."
greet(person: ["name": "Jane", "location": "Cupertino"])
// Prints "Hello Jane!"
// Prints "I hope the weather is nice in Cupertino."
defer 는 현재 scope 의 끝에 도달할 경우 코드를 실행시킴. Defer 내부 코드는 프로그램이 어떻게 해당 scope 를 벗어나는지와는 상관 없이 실행됨. (early exit / break / throw error 모두). 이는 메모리 할당/해제 등과 같이 쌍으로 존재해야 하는 액션이 있는 경우 유용.
먼저 정의된 defer 문이 나중에 실행됨.
Runtime error / crash 같이 프로그램 실행이 중지된 경우 defer 문은 실행아 안되지만 error throw 같은 경우에는 실행됨.
var score = 1
if score < 10 {
defer {
print(score)
}
score += 5
}
// Prints "6".
if score < 10 {
defer {
print(score)
}
defer {
print("The score is:")
}
score += 5
}
// Prints "The score is:".
// Prints "6".
특정 deployment target 에서 사용할 수 없는 API 를 사용하지 않도록 API availability checking 을 지원하고 있음
Compiler 는 SDK 의 availability 정보를 사용해서 코드의 모든 API 들이 프로젝트에 정의된 deployment target 에서 사용 가능한지 확인함. 만약 사용할 수 없는 API 가 있는 경우 compile 에러 발생.
Availability condition 을 사용해서 runtime 에서 API 사용 가능 여부에 따라 다르게 코드를 실행할 수 있음
if #available(iOS 10, macOS 10.12, *) {
// Use iOS 10 APIs on iOS, and use macOS 10.12 APIs on macOS
} else {
// Fall back to earlier iOS and macOS APIs
}
if #available(<#platform name#> <#version#>, <#...#>, *) {
<#statements to execute if the APIs are available#>
} else {
<#fallback statements to execute if the APIs are unavailable#>
}
@available(macOS 10.12, *)
struct ColorPreference {
var bestColor = "blue"
}
func chooseBestColor() -> String {
guard #available(macOS 10.12, *) else {
return "gray"
}
let colors = ColorPreference()
return colors.bestColor
}
unavailability condition 도 있음
if #available(iOS 10, *) {
} else {
// Fallback code
}
// 같은 코드
if #unavailable(iOS 10) {
// Fallback code
}