모듈, control flow

김태현·2022년 2월 18일
0

swift

목록 보기
4/7
post-thumbnail

Access Control - The Swift Programming Language (Swift 5.6)

모듈 : 코드 배포의 단위, import되는 것




Control Flow - The Swift Programming Language (Swift 5.6)

repeat while : are best used when the number of iterations isn’t known before the first iteration begins.

  • while evaluates its condition at the start of each pass through the loop.
  • repeat``while evaluates its condition at the end of each pass through the loop.

⇒ while은 앞부분에서 조건 판단, repeat while은 블럭 내부 수행 후 뒤에서 판단


you use the if statement to evaluate simple conditions with only a few possible outcomes. The switch statement is better suited to more complex conditions with multiple possible permutations and is useful in situations where pattern matching can help select an appropriate code branch to execute.

⇒ 경우의 수가 적을 때는 if, 경우의 수가 많고 패턴 매칭에는 switch


Every switch statement must be exhaustive.

⇒ 모든 경우가 고려되어야 한다.

default or enum의 경우는 모든 경우를 언급하면 default 필요 없음


switch statement finishes its execution as soon as the first matching switch case is completed, without requiring an explicit break statement.

case 끝에 break 필요없음

break 를 의도적으로 사용은 가능


The body of each case must contain at least one executable statement.

case 바디에 실행문 꼭 존재해야 한다.


To make a switch with a single case that matches both "a" and "A", combine the two values into a compound case, separating the values with commas.

⇒ a나 A를 만족하는 경우 표시는 case “a”, “A”:


value binding :

switch case can name the value or values it matches to temporary constants or variables, for use in the body of the case.

case let으로 case 실행문 내부에서 지역변수로 사용 가능


switch case can use a where clause to check for additional conditions.

⇒ case 에서 추가적인 조건을 확인할 수 있다.

case let (x, y) where x == y:
    print("(\(x), \(y)) is on the line x == y")

This default case doesn’t need to perform any action, and so it’s written with a single break statement as its body.

default에서 할 일이 없으면 break 작성


The fallthrough keyword simply causes code execution to move directly to the statements inside the next case

fallthrough 사용하면 다음 case 연달아 실행


you can nest loops and conditional statements inside other loops and conditional statements to create complex control flow structures.
useful to be explicit about which loop or conditional statement you want a break
statement to terminate. Similarly, if you have multiple nested loops, it can be useful to be explicit about which loop the continue
statement should affect.

⇒ 이중 반복문이나 조건문에서 명시적으로 명령문(break, continue) 대상에게 레이블 작성


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

Unlike an if statement, a guardstatement always has an else clause—the code inside the else clause is executed if the condition isn’t true.

guard의 목적은 빠른 exit 이고 반드시 else 작성

It can do this with a control transfer statement such as returnbreakcontinue, or throw, or it can call a function or method that doesn’t return, such as fatalError(_:file:line:).

guard의 else 문에서 반드시 exit해야 한다.

profile
iOS 공부 중

0개의 댓글