[Swift๐Ÿฆฉ] #5 ์ œ์–ด ํ๋ฆ„, #6 ํ•จ์ˆ˜

๋˜์ƒยท2022๋…„ 3์›” 21์ผ
0

iOS

๋ชฉ๋ก ๋ณด๊ธฐ
22/47
post-thumbnail

5. ์ œ์–ด ํ๋ฆ„

for, while, if, guard, switch, break, continue

๋‹ค๋ฅธ ์–ธ์–ด์™€ ๊ฑฐ์˜ ๋™์ผํ•˜๋‹ค๊ณ  ๋ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค.

1. For-In ๋ฃจํ”„

๋ฐฐ์—ด, ์ˆซ์ž ๋ฒ”์œ„, ๋ฌธ์ž์—ด, ๋”•์…”๋„ˆ๋ฆฌ, ... ์— for - in ๋ฃจํ”„๋ฅผ ์ ์šฉํ•ด์„œ ์š”์†Œ ํ•˜๋‚˜ํ•˜๋‚˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

์•ž ๋‚ด์šฉ์—์„œ ๋Œ€์ฒด๋กœ ๋‹ค๋ค˜์—ˆ๊ณ , ์ต์ˆ™ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด๊ฐ€ ๋„ˆ๋ฌด๋‚˜๋„ ์ž˜ ์žŠ์–ด๋ฒ„๋ฆฌ๋Š”

for (index, value) in enumarated(arr) {
}

for i in stride(from: 0, to: 10, by: 2) { 
    // 10 ์€ ํฌํ•จ ์•ˆ๋จ.
}

for i in stride(from: 0, through: 10, by: 2) { 
    // 10 ํฌํ•จ.
}

๋งŒ ์“ฐ๊ณ  ๋„˜์–ด๊ฐ€๋„๋ก ํ•˜๊ฒ ๋‹ค. ํŒŒ์ด์ฌ์—์„œ๋Š” enumerate(arr) / range(0, 10, 2) ์ด๋ผ ํ•ญ์ƒ ํ—ท๊ฐˆ๋ฆผ


2. while / repeat ~ while

๋‹ค๋ฅธ ์–ธ์–ด์˜ while, do ~ while ๊ณผ ๊ฐ™๋‹ค. ์กฐ๊ฑด์ด true ์ธ ๋™์•ˆ ๋ฐ˜๋ณต / ์ฒซ๋ฒˆ์งธ๋Š” ์กฐ๊ฑด ๋”ฐ์ง€์ง€ ์•Š๊ณ  ์‹คํ–‰ ํ›„ ๋ฐ˜๋ณต.


3. ์กฐ๊ฑด๋ฌธ

1. if ~ else if ~ else


2. switch โญ๏ธ

Swift switch ์˜ ํŠน์ง•

  • swift ๋Š” break ๋ฅผ ์ ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
    ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ๋Š” ์ผ€์ด์Šค๋งˆ๋‹ค break ๋ฅผ ๊ฑธ์–ด์ฃผ์ง€ ์•Š์œผ๋ฉด ๋‹ค์Œ๊ฒƒ๊นŒ์ง€ ๋ชจ๋‘ ์‹คํ–‰๋˜์ง€๋งŒ, Swift ๋Š” ์•„๋‹˜. ์˜คํžˆ๋ ค, ์›๋ž˜์ฒ˜๋Ÿผ ์•„๋ž˜ ์ผ€์ด์Šค๊นŒ์ง€ ์‹คํ–‰๋˜๊ฒŒ ํ•˜๊ณ  ์‹ถ์œผ๋ฉด, fallthrough ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์จ์ค€๋‹ค. โญ๏ธ
  • switch ๋ฌธ์— ์ •์ˆ˜๋‚˜ ๋ฌธ์ž๋งŒ์ด ์•„๋‹ˆ๋ผ, ํŠœํ”Œ, enum ๊ฐ™์€ ๊ฒƒ๋“ค์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
switch int(score / 10) {
case 10, 9: print("A") // , ๋กœ ๋‚˜๋ˆ„๊ธฐ๋„ ๊ฐ€๋Šฅ.
case 8: print("B")
case 7: print("C")
case 6: print("D")
default: print("F")
}

// ์ˆซ์ž ๋ฒ”์œ„๋กœ ์“ฐ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
switch score {
case 90...100: print("A")
case 80..<90: print("B")
case 70..<80: print("C")
case 60..<70: print("D")
default: print("F")
}

3. switch - tuple / where โญ๏ธ

case ๋ฅผ ๋ฌด์‹œํ•˜๋ ค๋ฉด _ ๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

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) \(y) 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"

๊ฐ’์„ ๋ฐ”์ธ๋”ฉํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ , where ์ ˆ์„ ์ด์šฉํ•ด์„œ ๋ฐ”์ธ๋”ฉํ•œ ๊ฐ’์˜ ์กฐ๊ฑด์„ ๋”ฐ์งˆ ์ˆ˜๋„ ์žˆ๋‹ค.

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) 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 "on the x-axis with an x value of 2"

4. ์ œ์–ด ๋ณ€๊ฒฝ ๊ตฌ๋ฌธ

continue, break, fallthrough, return(ํ•จ์ˆ˜), throw(์—๋Ÿฌ ํ•ธ๋“ค๋ง)

1. continue

ํ˜„์žฌ ๋ฐ˜๋ณตํ•˜๊ณ  ์žˆ๋Š” ๋‹จ๊ณ„๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ , ๋‹ค์Œ ๋ฐ˜๋ณต์„ ์‹œ์ž‘.

2. break โญ๏ธ

ํ˜„์žฌ ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ.
or switch ๋ฌธ์—์„œ break ์•ž์— ์žˆ๋Š” ์กฐ๊ฑด์„ ์ œ์™ธํ•œ ๊ฒƒ์„ ๋ฌด์‹œ. default: break

3. fallthrough

switch ๋ฌธ์ด C ์ฒ˜๋Ÿผ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๊ฒŒ, ๋‹ค์Œ ๋‹จ๊ณ„๋กœ ํ๋ฅด๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค.

+) ๋ผ๋ฒจ์„ ์ด์šฉํ•ด์„œ ํŠน์ • ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐˆ ์ˆœ ์žˆ์ง€๋งŒ, ์ž˜ ์•ˆ ์“ฐ์ž„.

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!")

5. Early Exit (guard) โญ๏ธ

ํ•จ์ˆ˜๋‚˜ ํด๋กœ์ € ๋‚ด์—์„œ, ์–ด๋–ค ์กฐ๊ฑด์„ ๋งŒ์กฑํ•ด์•ผํ•˜๋Š” ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ๋‹ค์Œ ๋ฌธ์žฅ์˜ ์‹คํ–‰์„ ๊ฒฐ์ •ํ•œ๋‹ค.
๐Ÿ“• ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹นํ•˜๋Š” ๋ฌธ์žฅ์„ ๊ฒ€์‚ฌํ•ด์„œ true ์ด๋ฉด ๋‹ค์Œ ๋ฌธ์žฅ์„ ์‹คํ–‰ํ•˜๊ณ , ๊ทธ ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ด ๋˜๋ฉด ์ƒ์œ„ ๋ธ”๋ก์„ ํƒˆ์ถœํ•˜๋Š” ๋ฌธ์žฅ์„ else ๋ฌธ์— ์ž‘์„ฑํ•œ๋‹ค.
๐Ÿ“• ๋ณดํ†ต์€ else ๋ฌธ์—์„œ return ์ด ์‚ฌ์šฉ๋˜์ง€๋งŒ, ์ƒํ™ฉ์— ๋”ฐ๋ผ break continue throw ๋ชจ๋‘ ์‚ฌ์šฉ ๊ฐ€๋Šฅ. ํƒˆ์ถœ์ด๋‹ˆ๊นŒ~
๐Ÿ“• ์˜ˆ์™ธ ์ƒํ™ฉ๋งŒ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ if ๋Œ€์‹  guard ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

func greet(person: [String: String]) {
    // ๋”•์…”๋„ˆ๋ฆฌ์— name ์ด ์—†์œผ๋ฉด ๊ทธ๋ƒฅ ๋๋‚จ.
    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).")
}
// OS ๋ฒ„์ ผ์—์„œ ๊ฐ€๋Šฅํ•œ API ์ธ์ง€ ์ฒดํฌํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•œ๋‹ค.
// guard ๋ฌธ์œผ๋กœ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
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
}



6. ํ•จ์ˆ˜

ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ ๋ธ”๋Ÿญ.
๋ณดํ†ต ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜๋ฉด์„œ ๋‘๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒƒ์„ ๋ฌถ์–ด์„œ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ ๋‹ค.

ํ•จ์ˆ˜ ์ด๋ฆ„, ์ธ์ž, ๋ฐ˜ํ™˜ ํƒ€์ž…, ๋ฐ˜ํ™˜๊ฐ’์ด ์žˆ๋Š” ํ˜•ํƒœ๋กœ ๋‹ค๋ฅธ ์–ธ์–ด์˜ ํ•จ์ˆ˜์™€ ๊ตฌ์กฐ๋Š” ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—, Swift ํ•จ์ˆ˜์˜ ํŠน์ดํ•œ ์ ๋งŒ ์ •๋ฆฌํ•˜๊ฒ ๋‹ค.

1. ์ธ์ž ์ด๋ฆ„์— ๋ผ๋ฒจ์„ ์ค€๋‹ค โญ๏ธ

func greet(person name: String) -> String {
    return "Hello, " + name + "!";
}

ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ฒจ์ค„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ด๋ฆ„๊ณผ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ํ•ด๋‹น ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด๋ฆ„์ด ๋‹ค๋ฅด๋‹ค.
ํ•จ์ˆ˜ ์™ธ๋ถ€์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„ ๋ผ๋ฒจ์„ ์ƒ๋žตํ•˜๊ณ  ์‹ถ์œผ๋ฉด _ ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ“• ์›๋ž˜ ์•ˆ๋˜๋Š” ํ‚ค์›Œ๋“œ๋„ `parametername` ์ฒ˜๋Ÿผ ` ๋ฅผ ๋ถ™์—ฌ์„œ ์ฃผ๋ฉด ๋Œ€๋ถ€๋ถ„์˜ ์ด๋ฆ„์„ ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

2. tuple? ์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

์‚ฌ์‹ค ์ด๊ฑด ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ๋„ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, tuple ์— ๋ผ๋ฒจ์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์€ ํŠน์ดํ•˜๋‹ค.
๊ทธ๋ฆฌ๊ณ  ํŠœํ”Œ์— ๋Œ€ํ•œ ์˜ต์…”๋„์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š”๊ฑด ํŠน์ดํ•˜๋‹ค.

func minMax(array: [Int]) -> (min: Int, max: Int) {
    var currentMin = array[0]
    var currentMax = array[0]
    for value in array[1..<array.count] {
        if value < currentMin {
            currentMin = value
        } else if value > currentMax {
            currentMax = value
        }
    }
    return (currentMin, currentMax)
}

let bounds = minMax(array: [8, -6, 2, 109, 3, 71])
print("min is \(bounds.min) and max is \(bounds.max)") 
// ๋ฐ˜ํ™˜ํ•˜๋Š” ํŠœํ”Œ์˜ ๊ฐ’์„ ๋ผ๋ฒจ ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉ.
// Prints "min is -6 and max is 109"

3. ํ•จ์ˆ˜ ๋ฐ”๋””๊ฐ€ ํ•œ์ค„์ด๋ฉด, ์•”์‹œ์  return ์ด ๋œ๋‹ค.

4. ๊ธฐ๋ณธ๊ฐ’์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

func addTwo(first: Int, second: Int = 2) {
    return first + second
}

addTwo(first: 1) // 3
addTwo(first: 10, second: 3) // 13

๐Ÿ“• ๊ธฐ๋ณธ ๊ฐ’์ด ์—†๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ธฐ๋ณธ ๊ฐ’์ด ์žˆ๋Š” ๊ฒƒ๋ณด๋‹ค ์•ž์— ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ณ ,
๊ธฐ๋ณธ ๊ฐ’ ์—ฌ๋ถ€์™€ ์ƒ๊ด€์—†์ด ๋” ์ค‘์š”ํ•œ ๊ฐ’์„ ์•ž์— ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

5. ๊ฐ€๋ณ€ ํŒŒ๋ผ๋ฏธํ„ฐ

๊ฐ™์€ ํƒ€์ž…์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ์„ ๋•Œ,
์ธ์ž ํƒ€์ž…์„ Type... ์œผ๋กœ ์ง€์ •ํ•ด์ฃผ๋ฉด, ํ•ด๋‹น ํƒ€์ž…์˜ 0๊ฐœ ์ด์ƒ์˜ ์ธ์ž๊ฐ€ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

func arithmeticMean(_ numbers: Double...) -> Double {
    var total: Double = 0
    for number in numbers {
        total += number
    }
    return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8.25, 18.75)
// returns 10.0, which is the arithmetic mean of these three numbers

6. inout ํŒŒ๋ผ๋ฏธํ„ฐ

ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋ณต์‚ฌ๊ฐ’์ด ๋ฐ”๋€Œ๋‹ˆ๊นŒ, C ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด ์ฃผ์†Œ๊ฐ’์„ ๋„˜๊ฒจ์„œ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ
inout ํ‚ค์›Œ๋“œ๋ฅผ ๋„ฃ์–ด์„œ, ํ•จ์ˆ˜ ๋ฐ–์˜ ๊ฐ’์ด ๋ฐ”๋€”์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

func swapTwoInts(_ a: inout Int, _ b: inout Int) {
    let temporaryA = a
    a = b
    b = temporaryA
}

var num1 = 0
var num2 = 1
swapTwoInts(&num1, &num2)

๐Ÿ“• ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจ๋Ÿฌ๋‹ค์ž„์—์„œ๋Š” ์ง€์–‘ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

  1. ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ ๋  ๋•Œ ์ „๋‹ฌ์ธ์ž์˜ ๊ฐ’์„ ๋ณต์‚ฌํ•˜๊ณ 
  2. ๋ณต์‚ฌํ•œ ๊ฒƒ์„ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝ
  3. ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋ฉด์„œ ๋ณ€๊ฒฝ๋œ ๊ฐ’์„ ์›๋ž˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ํ• ๋‹น

-> Memory Safety ๋ฅผ ๋ณด์žฅํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Œ.

๐Ÿ“• inout ์€ ๊ธฐ๋ณธ๊ฐ’ X, ๊ฐ€๋ณ€ ํŒŒ๋ผ๋ฏธํ„ฐ X


7. ํ•จ์ˆ˜ ํƒ€์ž…

ํ•จ์ˆ˜๋ฅผ type ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

func addTwoInts(_ a: Int, _ b: Int) -> Int {
    return a + b
}

// ๋ณ€์ˆ˜์— ํ•จ์ˆ˜๋ฅผ ํ• ๋‹น ๊ฐ€๋Šฅ.
var mathFunction: (Int, Int) -> Int = addTwoInts

์ธ์ž๋กœ ํ•จ์ˆ˜๋ฅผ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ์Œ.

func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
    print("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5)
// Prints "Result: 8"

ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ์Œ.

func stepForward(_ input: Int) -> Int {
    return input + 1
}
func stepBackward(_ input: Int) -> Int {
    return input - 1
}

func chooseStepFunction(backward: Bool) -> (Int) -> Int {
    return backward ? stepBackward : stepForward
}

8. ์ค‘์ฒฉ ํ•จ์ˆ˜

ํ•จ์ˆ˜ ์•ˆ์— ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ทธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ”์œ„๋Š” ์ปค๋‹ค๋ž€ ํ•จ์ˆ˜ ์•ˆ!

func chooseStepFunction(backward: Bool) -> (Int) -> Int {
    func stepForward(input: Int) -> Int { return input + 1 }
    func stepBackward(input: Int) -> Int { return input - 1 }
    return backward ? stepBackward : stepForward
}

๐Ÿ“• 9. ์ข…๋ฃŒ๋˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜ Never

  • ์ •์ƒ์ ์œผ๋กœ ๋๋‚˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜. == ๋น„๋ฐ˜ํ™˜ ํ•จ์ˆ˜
  • ์˜ค๋ฅ˜๋ฅผ ๋˜์ง„๋‹ค๋˜๊ฐ€, ์˜ค๋ฅ˜๋ฅผ ๋ณด๊ณ ํ•˜๊ณ  ํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒํ•ด๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ.
  • guard ๋ฌธ์˜ else ๋ธ”๋ก์—์„œ๋„ ํ˜ธ์ถœ ๊ฐ€๋Šฅ
  • ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ๊ฐ€ fatalError
func crashAndBurn() -> Never {
    fatalError("fatal error")
}

๐Ÿ“• 10. ๋ฐ˜ํ™˜ ๊ฐ’์„ ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜

  • ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ, ์ปดํŒŒ์ผ๋Ÿฌ ๊ฒฝ๊ณ ๋ฅผ ์—†์• ์ฃผ๋Š” ์ฝ”๋“œ @discardableResult
@discardableResult func discardableResultSay(_ something: String) -> String {
    print(something)
    return something
}

discardableResultSay("hi") // ๋ฐ˜ํ™˜๊ฐ’์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์ง€๋งŒ ์ปดํŒŒ์ผ๋Ÿฌ ๊ฒฝ๊ณ  ์•ˆ๋œธ.
profile
0๋…„์ฐจ iOS ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€