ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Control Flow
    iOS/Swift 공식문서 2022. 2. 15. 14:07

    For-In Loops

    for-in을 이용하여 array, 숫자의 범위, 문자열의 문자 등의 sequence에 대하여 반복문을 실행할 수 있다. for문에서 사용하려면 모두 Sequence 프로토콜을 따라야 한다.

    let names = ["Anna", "Alex", "Brian", "Jack"]
    for name in names {
        print("Hello, \(name)!")
    }
    
    for index in 1...5 { // index는 for문 안에서 constant로 취급
        print("\(index) times 5 is \(index * 5)")
    }

     

    반복문에서 dictionary의 각 pair는 (key, value) 튜플로 반환된다. 따라서 아래와 같이 decomposing이 가능하다.

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

     

    sequence 안의 값이 필요하지 않다면 밑줄(_)로 처리하면 된다.

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

     

    오른쪽 경곗값을 포함하려면 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 Loops

    while

    다른 언어에서의 while문처럼 각 pass가 시작되기 전 condition을 확인한다.

    while condition {
        statements
    }

     

    repeat-while

    다른 언어에서의 do...while과 같은 기능이다. 각 pass를 실행한 이후 condition을 확인한다.

    repeat {
        statements
    } while condition

     

    Conditional Statements

    if, else if, else

    다른 언어에서의 문법과 비슷하다.

    var 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

    다른 언어와는 달리 Swift에서의 switch문은 break가 필요없다. 그 외의 문법은 다른 언어와 유사하다. 다만 더 세부적인 조건에 따라 각 case의 코드를 일부만 실행하고 싶으면 break를 사용할 수 있다.

    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"

     

    Interval Matching

    값이 interval에 포함되는지도 확인할 수 있다.

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

     

    Tuples

    Tuple에서 여러 값을 테스트하기 위해 switch를 사용할 수 있다. 각각의 값은 특정 값이나 범위에 해당하는지 테스트할 수 있다. 특정 값이 matching이 필요없을 때는 밑줄(_)로 처리하면 된다.

    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"

     

    Value Bindings

    case문의 경우 테스트하는 값을 임시로 사용하는 상수/변수에 할당할 수 있는데, 이러한 기능을 Value Binding이라 한다. Value Binding을 사용하면 테스트하는 값을 case문의 body에서 사용할 수 있다.

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

     

    case문에서 추가적인 condition을 확인하기 위해 where clause를 사용할 수 있다.

    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들이 body를 공유할 수 있다. 각 case는 쉼표로 구분한다. 이 중 하나라도 맞으면 body가 실행된다.

    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"

     

    compound case도 value binding을 포함할 수 있다. 각 binding의 타입이 다 같아야 하므로 주의하자. 그래야 body에서 일관성 있게 사용할 수 있기 때문이다.

    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

    다른 언어의 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 in a loop statement

    다른 언어의 break와 기능이 동일하다.

    break in a switch statement

    case body의 실행을 끝내고 switch문 밖으로 나가게 해준다. Swift의 switch의 경우 첫번째로 match된 case의 실행이 완전히 끝나면 switch문의 실행이 종료된다. 따라서 break는 case body의 실행을 중간에 종료할 때 주로 사용한다. 혹은 아래 예시처럼 특정 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."

     

    Fallthrough

    Swift의 경우 기본적으로 match된 case 아래의 case들은 break가 없어도 실행되지 않는다. 코드 작성을 더 간결하게 만들기 위해서다. 따라서 아래의 case들도 실행되게 하기 위해서는 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

    중첩된 반복문이나 조건문에서 breakcontinue를 사용해야 할 때 사용한다.

    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

    guard 문의 경우 항상 else와 같이 사용해야 한다. Bool값이 true라면 guard문이 실행되지 않고, false라면 실행된다.

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

     

    Checking API Availability

    특정 deployment target에서 사용할 수 없는 API를 실수로 사용하지 않게 하기 위해 Swift에서 built-in support를 제공한다. #available()을 사용하면 된다. platform name와 version을 명시하여 괄호로 구분하여 나열하면 된다.

    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
    }

     

    Reference

    https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html

    'iOS > Swift 공식문서' 카테고리의 다른 글

    Closures  (0) 2022.02.16
    Functions  (0) 2022.02.15
    Collection Types  (0) 2022.02.14
    Strings & Characters  (0) 2022.02.14
    Basic Operators  (0) 2022.02.12

    댓글

Designed by Tistory.