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. Theswitch
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 matchingswitch
case is completed, without requiring an explicitbreak
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 :
A
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
실행문 내부에서 지역변수로 사용 가능
A
switch
case can use awhere
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 singlebreak
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 abreak
statement to terminate. Similarly, if you have multiple nested loops, it can be useful to be explicit about which loop thecontinue
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, aguard
statement always has anelse
clause—the code inside theelse
clause is executed if the condition isn’t true.
⇒ guard
의 목적은 빠른 exit 이고 반드시 else 작성
It can do this with a control transfer statement such as
return
,break
,continue
, orthrow
, or it can call a function or method that doesn’t return, such asfatalError(_:file:line:)
.
⇒ guard
의 else 문에서 반드시 exit해야 한다.