๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿฅ– Bread Basics/Swift

Swift ๊ณต์‹ ๋ฌธ์„œ ์ •๋ฆฌ - ์ œ์–ด ํ๋ฆ„ (Control Flow)

by BreadDev 2025. 4. 10.
728x90

์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ์—๋Š” Swift ์ œ์–ด ํ๋ฆ„(Control Flow)์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ œ์–ด ํ๋ฆ„์€ ์ฝ”๋“œ์˜ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๊ตฌ๋ฌธ๋“ค์„ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ์ด๋ฅผ ์ž˜ ํ™œ์šฉํ•˜๋ฉด ํšจ์œจ์ ์ด๊ณ  ์ฝ๊ธฐ ์‰ฌ์šด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Œ ์ œ์–ด ํ๋ฆ„ ์†Œ๊ฐœ

Swift๋Š” ๋‹ค์–‘ํ•œ ์ œ์–ด ํ๋ฆ„ ๊ตฌ๋ฌธ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค:

  • ๋ฃจํ”„: for-in, while, repeat-while
  • ์กฐ๊ฑด๋ฌธ: if, switch, guard
  • ์ œ์–ด ๋ณ€๊ฒฝ ๊ตฌ๋ฌธ: break, continue, fallthrough
  • ๊ธฐํƒ€: defer, ๊ฐ€์šฉ์„ฑ ํ™•์ธ ๋“ฑ

์ด๋Ÿฌํ•œ ๊ตฌ๋ฌธ๋“ค์„ ํ†ตํ•ด ์ฝ”๋“œ์˜ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ์ œ์–ดํ•˜๊ณ , ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉฐ, ๋ฐ˜๋ณต ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Œ For-In ๋ฃจํ”„

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)")
// "3 to the power of 10 is 59049"

๋‹ค์–‘ํ•œ ๋ฒ”์œ„ ์‚ฌ์šฉํ•˜๊ธฐ

// ๋ฐ˜์—ด๋ฆผ ๋ฒ”์œ„ ์—ฐ์‚ฐ์ž(..<)
for tickMark in 0..<60 {
    // 0๋ถ€ํ„ฐ 59๊นŒ์ง€ 60๋ฒˆ ์‹คํ–‰
}

// stride ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ
for tickMark in stride(from: 0, to: 60, by: 5) {
    // 0, 5, 10, ..., 55 (5์”ฉ ์ฆ๊ฐ€, 60์€ ํฌํ•จํ•˜์ง€ ์•Š์Œ)
}

// ๋‹ซํžŒ ๋ฒ”์œ„์™€ ํ•จ๊ป˜ stride ์‚ฌ์šฉํ•˜๊ธฐ
for tickMark in stride(from: 3, through: 12, by: 3) {
    // 3, 6, 9, 12 (3์”ฉ ์ฆ๊ฐ€, 12 ํฌํ•จ)
}

๋ฒ”์œ„ ์—ฐ์‚ฐ์ž:

  • a...b: a๋ถ€ํ„ฐ b๊นŒ์ง€(b ํฌํ•จ) - ๋‹ซํžŒ ๋ฒ”์œ„
  • a..<b: a๋ถ€ํ„ฐ b-1๊นŒ์ง€(b ์ œ์™ธ) - ๋ฐ˜์—ด๋ฆผ ๋ฒ”์œ„
  • stride(from:to:by:): ํŠน์ • ๊ฐ„๊ฒฉ์œผ๋กœ ๋ฒ”์œ„ ์ƒ์„ฑ(๋๊ฐ’ ์ œ์™ธ)
  • stride(from:through:by:): ํŠน์ • ๊ฐ„๊ฒฉ์œผ๋กœ ๋ฒ”์œ„ ์ƒ์„ฑ(๋๊ฐ’ ํฌํ•จ)

๐Ÿ“Œ While ๋ฃจํ”„

While

while ๋ฃจํ”„๋Š” ์กฐ๊ฑด์ด false๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ์ฝ”๋“œ ๋ธ”๋ก์„ ๋ฐ˜๋ณต ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค:

var square = 0
var diceRoll = 0

while square < 25 {
    diceRoll += 1
    if diceRoll == 7 { diceRoll = 1 }
    square += diceRoll

    if square < board.count {
        square += board[square]  // ๊ฒŒ์ž„ํŒ์˜ ํšจ๊ณผ ์ ์šฉ
    }
}
print("Game over!")

while ๋ฃจํ”„: ์กฐ๊ฑด์„ ๋จผ์ € ํ™•์ธํ•œ ํ›„ ์กฐ๊ฑด์ด true๋ฉด ์ฝ”๋“œ ๋ธ”๋ก์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

Repeat-While

repeat-while ๋ฃจํ”„๋Š” ๋จผ์ € ์ฝ”๋“œ ๋ธ”๋ก์„ ์‹คํ–‰ํ•œ ํ›„ ์กฐ๊ฑด์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค:

var square = 0
var diceRoll = 0

repeat {
    square += board[square]  // ๊ฒŒ์ž„ํŒ์˜ ํšจ๊ณผ ์ ์šฉ

    diceRoll += 1
    if diceRoll == 7 { diceRoll = 1 }
    square += diceRoll
} while square < 25

print("Game over!")

repeat-while ๋ฃจํ”„: ๋‹ค๋ฅธ ์–ธ์–ด์˜ do-while๊ณผ ์œ ์‚ฌํ•˜๋ฉฐ, ์ฝ”๋“œ ๋ธ”๋ก์„ ๋จผ์ € ์‹คํ–‰ํ•œ ํ›„ ์กฐ๊ฑด์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฝ”๋“œ ๋ธ”๋ก์ด ์ตœ์†Œ ํ•œ ๋ฒˆ์€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ ์กฐ๊ฑด๋ฌธ

If ๊ตฌ๋ฌธ

if ๊ตฌ๋ฌธ์€ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค:

var temperatureInFahrenheit = 30
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.")
}

Swift 5.9๋ถ€ํ„ฐ๋Š” if ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

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."
}

if ํ‘œํ˜„์‹: Swift 5.9์—์„œ ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ์œผ๋กœ, ์กฐ๊ฑด์— ๋”ฐ๋ฅธ ๊ฐ’์„ ์ง์ ‘ ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Switch ๊ตฌ๋ฌธ

switch ๊ตฌ๋ฌธ์€ ๊ฐ’์„ ์—ฌ๋Ÿฌ ํŒจํ„ด๊ณผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค:

let someCharacter: Character = "z"
switch someCharacter {
case "a":
    print("The first letter of the Latin alphabet")
case "z":
    print("The last letter of the Latin alphabet")
default:
    print("Some other character")
}
// "The last letter of the Latin alphabet"

Swift 5.9๋ถ€ํ„ฐ๋Š” switch ํ‘œํ˜„์‹๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค:

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"
}

Swift์˜ switch ๊ตฌ๋ฌธ์€ ๋‹ค๋ฅธ ์–ธ์–ด์™€ ๋‹ฌ๋ฆฌ ๋ช…์‹œ์  fallthrough๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค(๊ฐ case ์‹คํ–‰ ํ›„ ์ž๋™์œผ๋กœ ๋‹ค์Œ case๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š์Šต๋‹ˆ๋‹ค). ๋˜ํ•œ ๊ฐ case๋Š” ์ตœ์†Œ ํ•˜๋‚˜์˜ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๊ตฌ๋ฌธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์–‘ํ•œ ํŒจํ„ด ๋งค์นญ

  1. ๊ฐ„๊ฒฉ ๋งค์นญ:

    swift

    let approximateCount = 62
    switch approximateCount {
    case 0:
        print("no")
    case 1..<5:
        print("a few")
    case 5..<12:
        print("several")
    case 12..<100:
        print("dozens of")
    default:
        print("many")
    }
    // "dozens of"
  2. ํŠœํ”Œ ๋งค์นญ:

    swift

    let somePoint = (1, 1)
    switch somePoint {
    case (0, 0):
        print("Origin")
    case (_, 0):
        print("On the x-axis")
    case (0, _):
        print("On the y-axis")
    case (-2...2, -2...2):
        print("Inside the box")
    default:
        print("Outside the box")
    }
    // "Inside the box"
  3. ๊ฐ’ ๋ฐ”์ธ๋”ฉ:

    swift

    let anotherPoint = (2, 0)
    switch anotherPoint {
    case (let x, 0):
        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):
        print("Somewhere else at (\(x), \(y))")
    }
    // "On the x-axis with an x value of 2"
  4. where ์ ˆ ์‚ฌ์šฉ:

    swift

    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")
    }
    // "(1, -1) is on the line x == -y"
  5. ํ•ฉ์„ฑ ์ผ€์ด์Šค:

    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")
    }
    // "e is a vowel"

ํŒจํ„ด ๋งค์นญ: ๊ฐ’์„ ๋‹ค์–‘ํ•œ ํŒจํ„ด๊ณผ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•

์™€์ผ๋“œ์นด๋“œ ํŒจํ„ด(_): ์–ด๋–ค ๊ฐ’์ด๋“  ๋งค์นญ๋˜๋Š” ํŒจํ„ด

๊ฐ’ ๋ฐ”์ธ๋”ฉ: ๋งค์นญ๋œ ๊ฐ’์„ ์ž„์‹œ ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์— ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•

where ์ ˆ: ํŒจํ„ด ๋งค์นญ์— ์ถ”๊ฐ€ ์กฐ๊ฑด์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•

ํ•ฉ์„ฑ ์ผ€์ด์Šค: ์—ฌ๋Ÿฌ ํŒจํ„ด์„ ํ•˜๋‚˜์˜ case๋กœ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐฉ๋ฒ•

๐Ÿ“Œ ์ œ์–ด ๋ณ€๊ฒฝ ๊ตฌ๋ฌธ

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)
// "grtmndsthnklk"

continue: ํ˜„์žฌ ๋ฃจํ”„ ๋ฐ˜๋ณต์„ ์ค‘๋‹จํ•˜๊ณ  ๋‹ค์Œ ๋ฐ˜๋ณต์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.

Break

break ๊ตฌ๋ฌธ์€ ์ „์ฒด ์ œ์–ด ํ๋ฆ„ ๊ตฌ๋ฌธ์˜ ์‹คํ–‰์„ ์ฆ‰์‹œ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค:

// ๋ฃจํ”„์—์„œ break
let scores = [10, 20, 30, 40, 50]
var sum = 0
for score in scores {
    if score > 30 {
        break
    }
    sum += score
}
print(sum)  // 60 (10 + 20 + 30)

// switch์—์„œ break
let numberSymbol: Character = "ไธ‰"  // ์ค‘๊ตญ์–ด๋กœ 3
var possibleIntegerValue: Int?
switch numberSymbol {
case "1", "ูก", "ไธ€", "เน‘":
    possibleIntegerValue = 1
case "2", "ูข", "ไบŒ", "เน’":
    possibleIntegerValue = 2
case "3", "ูฃ", "ไธ‰", "เน“":
    possibleIntegerValue = 3
default:
    break
}

break:

  • ๋ฃจํ”„์—์„œ: ๋ฃจํ”„์˜ ์‹คํ–‰์„ ์ฆ‰์‹œ ์ข…๋ฃŒํ•˜๊ณ  ๋ฃจํ”„ ๋‹ค์Œ ์ฝ”๋“œ๋กœ ์ด๋™
  • switch์—์„œ: switch ๊ตฌ๋ฌธ์„ ์ฆ‰์‹œ ์ข…๋ฃŒ

Fallthrough

Swift์˜ switch ๊ตฌ๋ฌธ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ case์—์„œ ๋‹ค์Œ case๋กœ ์ž๋™์œผ๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š์ง€๋งŒ, 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)
// "The number 5 is a prime number, and also an integer."

fallthrough: switch ๋ฌธ์—์„œ ํ˜„์žฌ case ์‹คํ–‰ ํ›„ ๋‹ค์Œ case๋กœ ์‹คํ–‰์„ ๊ณ„์†ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ ๋ผ๋ฒจ์ด ์žˆ๋Š” ๊ตฌ๋ฌธ

๋ณต์žกํ•œ ์ œ์–ด ํ๋ฆ„์—์„œ๋Š” ํŠน์ • ๋ฃจํ”„๋‚˜ ์กฐ๊ฑด๋ฌธ์„ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ๋ผ๋ฒจ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

gameLoop: while square != finalSquare {
    diceRoll += 1
    if diceRoll == 7 { diceRoll = 1 }

    switch square + diceRoll {
    case finalSquare:
        // ๊ฒŒ์ž„ ์ข…๋ฃŒ
        break gameLoop
    case let newSquare where newSquare > finalSquare:
        // ์œ ํšจํ•˜์ง€ ์•Š์€ ์ด๋™
        continue gameLoop
    default:
        // ์œ ํšจํ•œ ์ด๋™
        square += diceRoll
        square += board[square]
    }
}
print("Game over!")

๋ผ๋ฒจ์ด ์žˆ๋Š” ๊ตฌ๋ฌธ: ๊ตฌ๋ฌธ์— ์‹๋ณ„์ž๋ฅผ ๋ถ™์—ฌ break๋‚˜ continue ๋“ฑ์˜ ์ œ์–ด ํ๋ฆ„ ๋ณ€๊ฒฝ ์‹œ ์–ด๋–ค ๊ตฌ๋ฌธ์— ์ ์šฉํ• ์ง€ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ ์ด๋ฅธ ์ข…๋ฃŒ (Guard)

guard ๊ตฌ๋ฌธ์€ ํŠน์ • ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜์ง€ ์•Š์œผ๋ฉด ํ˜„์žฌ ์ฝ”๋“œ ๋ธ”๋ก์—์„œ ๋น ์ ธ๋‚˜๊ฐ€๋„๋ก ํ•ฉ๋‹ˆ๋‹ค:

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"])
// "Hello John!"
// "I hope the weather is nice near you."

guard ๊ตฌ๋ฌธ์˜ ์žฅ์ :

  1. ํ•„์š”ํ•œ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜์ง€ ์•Š์œผ๋ฉด ์ผ์ฐ ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์œผ๋กœ ํ•ด์ œ๋œ ๊ฐ’์€ guard ์ดํ›„ ์ฝ”๋“œ์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

guard: ํŠน์ • ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜์–ด์•ผ ์ดํ›„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉฐ, ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜์ง€ ์•Š์œผ๋ฉด else ๋ธ”๋ก์„ ํ†ตํ•ด ํ˜„์žฌ ๋ฒ”์œ„๋ฅผ ๋น ์ ธ๋‚˜๊ฐ‘๋‹ˆ๋‹ค.

๐Ÿ“Œ ์—ฐ๊ธฐ๋œ ๋™์ž‘ (Defer)

defer ๋ธ”๋ก์€ ํ˜„์žฌ ๋ฒ”์œ„๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ ์‹คํ–‰๋  ์ฝ”๋“œ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค:

var score = 1
if score < 10 {
    defer {
        print(score)
    }
    score += 5
}
// "6" ์ถœ๋ ฅ

์—ฌ๋Ÿฌ defer ๋ธ”๋ก์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰์— ์ •์˜๋œ ๋ธ”๋ก๋ถ€ํ„ฐ ์—ญ์ˆœ์œผ๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค:

if score < 10 {
    defer {
        print(score)
    }
    defer {
        print("The score is:")
    }
    score += 5
}
// "The score is:" ์ถœ๋ ฅ
// "6" ์ถœ๋ ฅ

defer: ํ˜„์žฌ ๋ฒ”์œ„๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ ์‹คํ–‰๋  ์ฝ”๋“œ ๋ธ”๋ก์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ž์› ํ•ด์ œ, ํŒŒ์ผ ๋‹ซ๊ธฐ ๋“ฑ ์ •๋ฆฌ ์ž‘์—…์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ API ๊ฐ€์šฉ์„ฑ ํ™•์ธ

ํŠน์ • API๊ฐ€ ์‹คํ–‰ ์ค‘์ธ iOS/macOS ๋ฒ„์ „์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค:

if #available(iOS 10, macOS 10.12, *) {
    // iOS 10 ์ด์ƒ ๋˜๋Š” macOS 10.12 ์ด์ƒ์—์„œ๋งŒ ์‹คํ–‰
} else {
    // ์ด์ „ ๋ฒ„์ „์—์„œ ์‹คํ–‰ํ•  ๋Œ€์ฒด ์ฝ”๋“œ
}

Swift 5.6๋ถ€ํ„ฐ๋Š” ๋ฐ˜๋Œ€๋กœ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค:

if #unavailable(iOS 10) {
    // iOS 10 ๋ฏธ๋งŒ์—์„œ๋งŒ ์‹คํ–‰
}

#available: ์ฝ”๋“œ๊ฐ€ ํŠน์ • ํ”Œ๋žซํผ ๋ฒ„์ „ ์ด์ƒ์—์„œ๋งŒ ์‹คํ–‰๋˜๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.

#unavailable: ์ฝ”๋“œ๊ฐ€ ํŠน์ • ํ”Œ๋žซํผ ๋ฒ„์ „ ๋ฏธ๋งŒ์—์„œ๋งŒ ์‹คํ–‰๋˜๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ ์ •๋ฆฌ

Swift์˜ ์ œ์–ด ํ๋ฆ„ ๊ตฌ๋ฌธ์€ ์ฝ”๋“œ์˜ ์‹คํ–‰ ํ๋ฆ„์„ ํšจ์œจ์ ์œผ๋กœ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ํŠนํžˆ Swift๋งŒ์˜ ํŠน์ง•์ ์ธ ์š”์†Œ๋“ค์„ ์ •๋ฆฌํ•˜์ž๋ฉด:

  • for-in ๋ฃจํ”„๋Š” ๋‹ค์–‘ํ•œ ์ปฌ๋ ‰์…˜๊ณผ ๋ฒ”์œ„๋ฅผ ์‰ฝ๊ฒŒ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.
  • switch ๊ตฌ๋ฌธ์€ ํŒจํ„ด ๋งค์นญ์ด ๋งค์šฐ ๊ฐ•๋ ฅํ•˜๋ฉฐ, ์ž๋™ fallthrough๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
  • guard ๊ตฌ๋ฌธ์€ ์กฐ๊ฑด ๊ฒ€์‚ฌ์™€ ์ด๋ฅธ ์ข…๋ฃŒ๋ฅผ ํ†ตํ•ด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์—ฌ์ค๋‹ˆ๋‹ค.
  • defer ๋ธ”๋ก์€ ์ฝ”๋“œ ์‹คํ–‰ ๋ฒ”์œ„๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ ์ •๋ฆฌ ์ž‘์—…์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.
  • API ๊ฐ€์šฉ์„ฑ ํ™•์ธ์€ ๋‹ค์–‘ํ•œ OS ๋ฒ„์ „์„ ์ง€์›ํ•˜๋Š” ์•ฑ ๊ฐœ๋ฐœ์— ํ•„์ˆ˜์ ์ž…๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์ œ์–ด ํ๋ฆ„ ์š”์†Œ๋“ค์„ ์ ์ ˆํžˆ ์กฐํ•ฉํ•˜๋ฉด, ๋” ์•ˆ์ „ํ•˜๊ณ  ํšจ์œจ์ ์ด๋ฉฐ ์ฝ๊ธฐ ์‰ฌ์šด Swift ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.