ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • ์ œ์–ด๋ฌธ ( Control Flow )
    Swift 2022. 7. 26. 19:51

    ์•ˆ๋…•ํ•˜์„ธ์š” ์˜ค๋Š˜์€ Swift์—์„œ ์ œ๊ณตํ•˜๋Š” ์ œ์–ด๋ฌธ์— ๊ด€ํ•˜์—ฌ ์ ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿคจ

     

    ๐Ÿ“š๊ณต์‹๋ฌธ์„œ ๋ฐ ๋งํฌ

     

     

    Control Flow — The Swift Programming Language (Swift 5.7)

    Control Flow Swift provides a variety of control flow statements. These include while loops to perform a task multiple times; if, guard, and switch statements to execute different branches of code based on certain conditions; and statements such as break a

    docs.swift.org

     


     

    Swift์—์„œ ์ œ๊ณตํ•˜๋Š” ์ œ์–ด๋ฌธ์€ 

    • while loop
    • if guard
    • switch
    • for-in

    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!

    Dictionary์—์„œ ๋ฐ˜ํ™˜๋œ ํ‚ค-๊ฐ’ ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ ํŠœํ”Œ์„ ์ˆœํšŒํ•˜๋ฉฐ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
    for (animalName, legCount) in numberOfLegs {
        print("\(animalName)s have \(legCount) legs")
    }
    // ants have 6 legs
    // spiders have 8 legs
    // cats have 4 legs

    Dictionaryํ˜•์€ ์ˆœ์„œ๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์œผ๋ฉฐ ๋ฐ˜๋ณตํ•˜๋‹ค๊ณ  ํ•ด์„œ ๊ฒ€์ƒ‰๋˜๋Š” ์ˆœ์„œ๊ฐ€ ๋ณด์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 

    ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์ˆซ์ž ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด ์ˆœํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    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"

    ๋ฒ”์œ„ ์—ฐ์‚ฐ์ž๋กœ ์‚ฌ์šฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

    let minutes = 60
    for tickMark in 0..<minutes {
        // render the tick mark each minute (60 times)
    }
    
    //0๋ถ€ํ„ฐ 59๊นŒ์ง€ ์ด 60๋ฒˆ ๋ฐ˜๋ณต

    stride(from:to:by:) ํ•จ์ˆ˜๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ๋”๋ณด๊ธฐ

    stride(from:to:by:) : ์ง€์ •๋œ ์–‘๋งŒํผ ๋‹จ๊ณ„์ ์œผ๋กœ ์‹œ์ž‘ ๊ฐ’์—์„œ ๋ ๊ฐ’๊นŒ์ง€์˜ ์‹œํ€€์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

    ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ๋Š”

    • from start - ์‹œํ€€์Šค์— ์‚ฌ์šฉํ•  ์‹œ์ž‘ ๊ฐ’์ž…๋‹ˆ๋‹ค. ์‹œํ€€์Šค์— ๊ฐ’์ด ํฌํ•จ๋œ ๊ฒฝ์šฐ ์ฒซ ๋ฒˆ์จฐ ๊ฐ’์€ start์ž…๋‹ˆ๋‹ค.
    • to end - ์‹œํ€€์Šค๋ฅผ ์ œํ•œํ•˜๋Š” ๋ ๊ฐ’์ž…๋‹ˆ๋‹ค. end ๊ฒฐ๊ณผ ์‹œํ€€์Šค์˜ ์š”์†Œ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.
    • by stride - ๊ฐ ๋ฐ˜๋ณต์—์„œ ๋‹จ๊ณ„๋ณ„๋กœ ์ˆ˜ํ–‰ํ•  ์–‘์ž…๋‹ˆ๋‹ค. ์–‘์ˆ˜๋Š” stride์œ„์ชฝ์œผ๋กœ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค. ์Œ์ˆ˜๋Š” stride์•„๋ž˜์ชฝ์œผ๋กœ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค.
    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)
    }
    //0๋ถ€ํ„ฐ 5์”ฉ ์ฆ๊ฐ€ํ•˜์—ฌ minutes๊นŒ์ง€ ๋ฐ˜๋ณต์„ํ•ฉ๋‹ˆ๋‹ค. minutes๊ฐ’์€ 60์ด๋ฏ€๋กœ 55๊นŒ์ง€ ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค.
    
    
    //stride(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๋ฌธ์€ ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๋•Œ๊นŒ์ง€ ๊ตฌ๋ฌธ์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

    while ์กฐ๊ฑด {
    	code
    }
    
    let value = true
    while value {
        print("๋ฌดํ•œ์œผ๋กœ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค")
    }
    var num = 0
    while num < 5 {
    	print( " \(num) times ")
        num += 1
    }
    // 0 times 
    // 1 times 
    // 2 times 
    // 3 times 
    // 4 times

     

    Repeat-While ๋ฌธ

    repeat-While๋ฌธ์€ ๋‹ค๋ฅธ ์–ธ์–ด์˜ do-while๋ฌธ๊ณผ ์œ ์‚ฌํ•œ while๋ฌธ์ž…๋‹ˆ๋‹ค.

    ๊ตฌ๋ฌธ์„ ์ตœ์†Œํ•œ ๋ฒˆ ์‹คํ–‰ํ•˜๊ณ  while์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

    var num = 1
    let value = true
    
    repeat {
        print(num)
    } while value
    
    //repeat while๋ฌธ์€ ์ตœ์†Œ ํ•œ๋ฒˆ์€ ์‹คํ–‰๋˜์ง€๋งŒ ์กฐ๊ฑด์ด true์ด๋ฏ€๋กœ ๋ฌดํ•œ์œผ๋กœ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค.
    var a = 5
    var b = 10
    
    repeat {
    	a += 1
    	print( " number a is increased to \(a) " )
    } while a < b
    
    // number a is increased to 6 
    // number a is increased to 7 
    // number a is increased to 8 
    // number a is increased to 9 
    // number a is increased to 10

     

    ์กฐ๊ฑด์  ๊ตฌ๋ฌธ ( Conditional Statements )

    Swift์—์„œ๋Š” if ์™€ switch๋ฌธ ๋‘ ๊ฐ€์ง€ ์กฐ๊ฑด ๊ตฌ๋ฌธ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

     

    If ๋ฌธ

    if๋ฌธ์€ ์–ด๋– ํ•œ ๊ฐ’์ด ํŠน์ • ์กฐ๊ฑด์— ํ•ด๋‹นํ•  ๋•Œ๋งŒ ์„ ํƒ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

     

    if๋งŒ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ

    var temperatureInFahrenheit = 30
    if temperatureInFahrenheit <= 32 {
        print("It's very cold. Consider wearing a scarf.")
    }
    // Prints "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.")
    }
    // Prints "It's not that cold. Wear a t-shirt."
    // 40<=32 ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜์ง€ ์•Š์•„ else ๊ตฌ๋ฌธ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

    if else 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."
    //else-if ์กฐ๊ฑด์ธ 90>=86 ์กฐ๊ฑด์— ๋งž์•„ ๊ตฌ๋ฌธ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

    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.")
    }
    // ๋งž๋Š” ์กฐ๊ฑด์— ์—†์–ด ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

     

    Switch

    break๊ฐ€ ํ•„์ˆ˜์ ์ด์ง€ ์•Š์ง€๋งŒ case ์•ˆ์— ํŠน์ • ์ง€์ ์—์„œ ๋ฉˆ์ถ”๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด break์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    case์•ˆ์—๋Š” ์ตœ์†Œ ํ•˜๋‚˜์˜ ์‹คํ–‰ ๊ตฌ๋ฌธ์ด ๋ฐ˜๋“œ์‹œ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    switch๋ฌธ์˜ ๊ธฐ๋ณธ ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค.

    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๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    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"

    ์œ„์—์„œ ๋งํ–ˆ๋‹ค์‹œํ”ผ switch๊ตฌ๋ฌธ์€ ์•”์‹œ์ ์ธ ์ง„ํ–‰์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

    switch๊ตฌ๋ฌธ์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  case๋ฅผ ์ˆœํšŒํ•˜์—ฌ default๋ฅผ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

    let anotherCharacter: Character = "a"
    switch anotherCharacter {
    case "a": 
    case "A":
        print("The letter A")
    default:
        print("Not the letter A")
    }
    //case์•ˆ์—๋Š” ์ตœ์†Œ ํ•˜๋‚˜์˜ ์‹คํ–‰ ๊ตฌ๋ฌธ์ด ๋ฐ˜๋“œ์‹œ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    // ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒ!

    ์ฝ”๋“œ๋ฅผ ์ข€ ๋” ํšจ์œจ์ ์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. case ์•ˆ์— ์ฝค๋งˆ ( , )๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ case ์กฐ๊ฑด์„ ํ˜ผํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    let anotherCharacter: Character = "a"
    switch anotherCharacter {
    case "a", "A":
        print("The letter A")
    default:
        print("Not the letter A")
    }
    // Prints "The letter A"

     

    ์ธํ„ฐ๋ฒŒ ๋งค์นญ ( Interval Matching )

    ์ˆซ์ž์˜ ํŠน์ • ๋ฒ”์œ„๋ฅผ ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    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).")
    // ์กฐ๊ฑด์ธ 12..<100์— ๋งŒ์กฑํ•˜์—ฌ naturalCount์— "dozens of" ์„ ์–ธ๋ฉ๋‹ˆ๋‹ค.
    // Prints "There are dozens of moons orbiting Saturn."

     

    ํŠœํ”Œ ( Tuple )

    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"

    ์กฐ๊ฑด์ธ ( -2...2 , -2...2 )์— ๋งŒ์กฑํ•˜์—ฌ ๊ตฌ๋ฌธ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

     

    Where๋ฌธ

    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"

     

    ํ˜ผํ•ฉ ์ผ€์ด์Šค ( Compound Cases )

    case์— ์ฝค๋งˆ ( , )๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์—ฌ๋Ÿฌ ์กฐ๊ฑด์„ ํ˜ผํ•ฉํ•˜์—ฌ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

    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 )

    ์ฝ”๋“œ์˜ ์ง„ํ–‰์„ ๊ณ„์†ํ• ์ง€ ๋ง์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๊ฑฐ๋‚˜ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ์˜ ํ๋ฆ„์„ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    • continue
    • break
    • fallthrough
    • return 
    • throw

    continue๋ฌธ

    continue๋ฌธ์€ ํ˜„์žฌ loop๋ฅผ ์ค‘์ง€ํ•˜๊ณ  ๋‹ค์Œ 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
        } else {
            puzzleOutput.append(character)
        }
    }
    print(puzzleOutput)
    // Prints "grtmndsthnklk"

    break๋ฌธ

    break๋ฌธ์€ ์ „์ฒด ์ œ์–ด๋ฌธ์˜ ์‹คํ–‰์„ ์ฆ‰๊ฐ ์ค‘์ง€์‹œํ‚ต๋‹ˆ๋‹ค. break๋ฌธ์€ loop๋‚˜ switch๋ฌธ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

    let numberSymbol: Character = "ไธ‰"  // ์ค‘๊ตญ์–ด๋กœ 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 could not be found for \(numberSymbol).")
    }

    fallthrough๋ฌธ

    fallthroughํ‚ค์›Œ๋“œ๋Š” ์ดํ›„์˜ case์— ๋Œ€ํ•ด์„œ๋„ ์‹คํ–‰ํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. Swift์—์„œ๋Š” ์กฐ๊ฑด์— ๋งž๋Š” case์„ ์‹คํ–‰ํ•˜๋ฉด 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."

    โ€ป fallthrough๋Š” case ์กฐ๊ฑด์„ ํ™•์ธํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋ƒฅ ๋‹ค์Œ  case๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

    ๊ตฌ๋ฌธ ๋ ˆ์ด๋ธ”

    ๋ฐ˜๋ณต๋ฌธ์ด๋‚˜ ์กฐ๊ฑด๋ฌธ ๋“ฑ ํŠน์ • ๊ตฌ๋ฌธ์— ๋ ˆ์ด๋ธ”์„ ๋ถ™์—ฌ ๊ธฐ์–ตํ•˜๊ฒŒ ๋งŒ๋“ค๋ฉฐ break, continue๊ตฌ๋ฌธ์ด ์‚ฌ์šฉ๋  ๋•Œ ๋งŒ๋“  ๋ ˆ์ด๋ธ”์„ ๋ช…์‹œํ•ด์คŒ์œผ๋กœ์จ ์›ํ•˜๋Š” ๊ตฌ๋ฌธ ์œ„์น˜์— ์ •ํ™•ํžˆ ํ๋ฆ„ ์ œ์–ด๊ฐ€ ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค.

    label name: while condition {
        statements
    }
    aLable : for i in 1..<10{
    	bLable : for j in 1..<5{
        	if( j == 4 ){
            	break aLable
            }
             print(" i:\(i) j:\(j) ")
        }		
    }
    // i:1 j:1 
    // i:1 j:2 
    // i:1 j:3

     

    ์ด๋ฅธ ํƒˆ์ถœ ( Early Exit )

    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"])
    // Prints "Hello John!"
    // Prints "I hope the weather is nice near you."
    // ์ฒซ๋ฒˆ์งธ guard ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜์—ฌ Prints "Hello John!"์„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜์—ˆ์œผ๋ฉฐ 
    // ๋‘๋ฒˆ์จฐ guard ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜์—ฌ else ๊ตฌ๋ฌธ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
    
    greet(person: ["name": "Jane", "location": "Cupertino"])
    // Prints "Hello Jane!"
    // Prints "I hope the weather is nice in Cupertino."
    // ์ฒซ๋ฒˆ์งธ guard ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜์—ฌ  Prints "Hello Jane!""์„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋˜์—ˆ์œผ๋ฉฐ 
    // ๋‘๋ฒˆ์จฐ guard // Prints "I hope the weather is nice near you." ์„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

     

    ์ด์šฉ ๊ฐ€๋Šฅํ•œ API ๋ฒ„์ „ ํ™•์ธ ( Checking API Availability )

    Swift์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํŠน์ • ํ”Œ๋žซํผ ( ios , macOS, tvOS, watchOS )๊ณผ ํŠน์ • ๋ฒ„์ „์„ ํ™•์ธํ•˜๋Š” ๊ตฌ๋ฌธ์„ ์ œ๊ณตํ•ด์ค๋‹ˆ๋‹ค. ์ด ๊ตฌ๋ฌธ์„ ํ™œ์šฉํ•ด ํŠน์ • ํ”Œ๋žซํผ๊ณผ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๊ธฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ๋”ฐ๋กœ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    if #available(platform name version, ..., *) {
        statements to execute if the APIs are available
    } else {
        fallback statements to execute if the APIs are unavailable
    }
    
    //์‹ค์ œ ์‚ฌ์šฉ ์˜ˆ์ œ
    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
    }

     


    Swift์—์„œ ์ œ๊ณตํ•˜๋Š” ์ œ์–ด๋ฌธ์— ๊ด€ํ•˜์—ฌ ์ •๋ฆฌ๋ฅผ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ์ •๋ฆฌ๋ฅผ ํ•˜๋ฉด์„œ ๊ตฌ๋ฌธ ๋ ˆ์ด๋ธ”๋ผ๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ ์ฒ˜์Œ ์•Œ๊ฒŒ ๋˜์–ด

    ๋‹ค์‹œ ํ•œ๋ฒˆ ๋‚ด์šฉ์„ ๊ณต๋ถ€ํ•ด์•ผ ํ• ๊บผ๊ฐ™์Šต๋‹ˆ๋‹คใ…‹ใ…‹

Designed by Tistory.