Swift 에서는 다양한 제어 흐름이 있다. 여러번 작업을 수행하는 while
, 조건에 따라 다르게 실행되는 if
, guard
, switch
구문과 코드에서 실행 플로우를 전달하는 break
, continue
가 있다. 또한 반복을 쉽게 만들어주는 for-in
, 현재 범위를 벗어날 때 수행되는 코드인 defer
가 있다.
For-In 루프 (For-In Loops)
연속된 것에 대해 for-in
루프를 사용하여 반복할 수 있다.
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
print("Hello, \(name)!")
}
// Hello, Anna!
// Hello, Alex!
// Hello, Brian!
// Hello, Jack!
딕셔너리의 경우 순서가 없다.
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
print("\(animalName)s have \(legCount) legs")
}
// cats have 4 legs
// ants have 6 legs
// spiders have 8 legs
숫자의 범위에 대해서도 쓸 수 있다. 보통 많은 반복문들은 이렇게 숫자의 범위를 사용하여 사용하곤 한다.
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
각 값을.사용하지 않는다면 _
를 사용하여 각 값을 무시할 수 있다.
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:)
함수를 사용하면 값을 건너뛸 수 있다.
let minutes = 60
for tickMark in 0..<minutes {
// render the tick mark each minute (60 times)
}
let minuteInterval = 5
for tickMark in stride(from: 0, to: minutes, by: minuteInterval) {
// render the tick mark every 5 minutes (0, 5, 10, 15 ... 45, 50, 55)
}
아래와 같이 strude(from:through:by:)
로 닫힌 범위도 가능하다.
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)
}
While 루프 (While Loops)
while
루프는 조건이 false
가 될 때까지 반복한다. 두종류의 while
루프가 있다.
while
: 루프가 시작할 때마다 조건을 비교한다.repeat-while
은 루프가 끝날 때 마다 조건을 비교한다.
while <#condition#> {
<#statements#>
}
repeat {
<#statements#>
} while <#condition#>
조건 구문 (Conditional Statements)
조건을 추가하는 방법은 if
구문과 switch
구문으로 2가지가 있다. if
구문은 간단한 조건, switch
의 경우는 조건이 여러가지 일 경우 유용하다.
If
else
절은 제외 가능하다.
var temperatureInFahrenheit = 30
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
}
// Prints "It's very cold. Consider wearing a scarf."
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.")
}
// Prints "It's not that cold. Wear a t-shirt."
else 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 {
print("It's not that cold. Wear a t-shirt.")
}
// Prints "It's really warm. Don't forget to wear sunscreen."
Switch
모든 switch
구문은 각각 case
키워드로 시작하는 여러개의 케이스로 이루어져 있다. 고려중인 타입의 모든 값은 switch
케이스 중 하나와 일치해야 한다. 그렇기 때문에 default
키워드는 마지막에 위치하면서 해결되지 않은 모든 값을 처리 하게 된다.
let someCharacter: Character = "z"
switch someCharacter {
case "a":
print("The first letter of the alphabet")
case "z":
print("The last letter of the alphabet")
default:
print("Some other character")
}
// Prints "The last letter of the alphabet"
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."
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"
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) is not a vowel or a consonant")
}
// Prints "e is a vowel"
let stillAnotherPoint = (9, 0)
switch stillAnotherPoint {
case (let distance, 0), (0, let distance):
print("On an axis, \(distance) from the origin")
default:
print("Not on an axis")
}
// Prints "On an axis, 9 from the origin"
제어 변경 구문 (Control Transfer Statements)
Swift 에서는 continue
, break
, fallthrough
, return
, throw
와 같이 5가지의 제어 변경 구문이 있다.
Continue
continue
는 반복구문을 이어나가게 한다.
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"
Break
break
는 구문을 즉시 종료한다.
Fallthrough
switch
에서 케이스를 만족하게 된다면 다음 케이스로 넘어가지않지만 fallthrough
를 쓰게된다면 다음케이스로 넘어간다.
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."
라벨이 있는 구문 (Labeled Statements)
여러가지 조건 구문들을 중첩할 때, break
나 continue
뒤에 라벨을 붙이는 것이 유용할 수 있다.
<#label name#>: while <#condition#> {
<#statements#>
}
아래와 같이 while
구문에 gameLoop
라는 라벨을 붙여줘서 break gameLoop
를 통해 전체 구문을 종료할 수 있게 해준다.
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!")
이른 종료 (Early Exit)
if
와 같이 guard
는 표현식의 Bool
값에 따라 실행여부를 결정한다. guard
는 항상 else
와 함께한다.
func greet(person: [String: String]) {
guard let name = person["name"] else {
return
}
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."
연기된 동작 (Deferred Actions)
defer
는 구문이 종료되기 전에 실행된다. 어떻게 종료하는지에 관계없지 defer
안의 코드는 항상 수행된다.
var score = 1
if score < 10 {
defer {
print(score)
}
score += 5
}
// Prints "6"
var score = 3
if score < 100 {
score += 100
defer {
score -= 100
}
// Other code that uses the score with its bonus goes here.
print(score)
}
// Prints "103"
하나 이상의 defer
를 사용하게 되면 첫번째 defer
이 마지막에 수행된다.
if score < 10 {
defer {
print(score)
}
defer {
print("The score is:")
}
score += 5
}
// Prints "The score is:"
// Prints "6"
Note: defer을 언제 활용할 수 있을까?
사용 가능한 API 확인 (Checking API Availability)
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
}
위의 코드를 살펴보면 iOS 10, macOS 10.12 이상 버전에서 실행된다. 마지막 인수인 *
는 필수이다.
if #available(iOS 10, *) {
} else {
// Fallback code
}
if #unavailable(iOS 10) {
// Fallback code
}
위의 코드에서는 iOS 10 버전보다 낮은 경우 실행되는 코드이다.
if #available(<#platform name#> <#version#>, <#...#>, *) {
<#statements to execute if the APIs are available#>
} else {
<#fallback statements to execute if the APIs are unavailable#>
}
'Swift > Swift 공식 문서' 카테고리의 다른 글
[Swift] 함수 (Functions) (0) | 2023.05.21 |
---|---|
[Swift] 콜렉션 타입 (Collection Types) (1) | 2023.05.19 |
[Swift] 문자열과 문자 (Strings and Characters) (1) | 2023.05.18 |
[Swift] 기본연산자 (Basic Operators) (0) | 2023.05.18 |
[Swift] 구조체와 클래스 (Structures and Classes) (0) | 2023.04.30 |