210304 Thu

Sunny·2021년 4월 3일
0

Today I Learned

목록 보기
7/88
post-thumbnail

if let vs. guard let

if let firstNumber = Int("4"), let secondNumber = Int("42"), firstNumber < secondNumber && secondNumber < 100 {
    print("\(firstNumber) < \(secondNumber) < 100")
}
// Prints "4 < 42 < 100"

if let firstNumber = Int("4") {
    if let secondNumber = Int("42") {
        if firstNumber < secondNumber && secondNumber < 100 {
            print("\(firstNumber) < \(secondNumber) < 100")
        }
    }
}
// Prints "4 < 42 < 100"

똑같은 결과가 산출되지만 여러개의 조건을 적어줄거라면 아래처럼 나눠 적어주는 것이 가독성이 훨씬 좋다.

스위프트 공식 문서 예제를 변형해서 연습해봄

  • else 안에 명령문은 guard에서의 조건이 false일 경우 실행됨
  • You use a guard statement to require that a condition must be true in order for the code after the guard statement to be executed.
  • Unlike an if statement, a guard statement always has an else clause—the code inside the else clause is executed if the condition isn’t true.

TheBasics
ControlFlow

continue vs. return vs. break

Continue

The continue statement tells a loop to stop what it’s doing and start again at the beginning of the next iteration through the loop. It says “I am done with the current loop iteration” without leaving the loop altogether.
if문 조건에 안 맞는다고 해서 for문을 아예 나가버리는 것이 아님.
그러니 다시 다음 조건문 케이스 실행하는 것.

The following example removes all vowels and spaces from a lowercase string to create a cryptic puzzle phrase.

puzzleInput에서 자음이 포함돼있다면 if 바깥으로 나가지 않고 다음 반복문 (다음 문자값 체크. 자음이 포함돼있는지 아닌지 계속 쭉쭉 진행해라가 continue의 의미)

If문 밖으로 나오는 경우는 조건에 안 맞을때 (= 여기선 자음에 포함되지 않은 모음을) puzzleOutput에 더해라

The code above calls the continue keyword whenever it matches a vowel or a space, causing the current iteration of the loop to end immediately and to jump straight to the start of the next iteration.

Return

return은 말 그대로 값을 돌려주는 것
여기서는 값을 String으로 되돌려주겠다 정의함.

The definition describes what the function does, what it expects to receive, and what it returns when it’s done. The definition makes it easy for the function to be called unambiguously from elsewhere in your code:

The body of the greet(person:) function starts by defining a new String constant called greeting and setting it to a simple greeting message. This greeting is then passed back out of the function using the return keyword. In the line of code that says return greeting, the function finishes its execution and returns the current value of greeting.

Break

break는 말그대로 당장 그 전체 control flow (e.g. a loop or switch statement)에서 하던 일 멈추고 나오라는 의미.

The break statement ends execution of an entire control flow statement immediately. The break statement can be used inside a switch or loop statement when you want to terminate the execution of the switch or loop statement earlier than would otherwise be the case.

Break in a Loop Statement
When used inside a loop statement, break ends the loop’s execution immediately and transfers control to the code after the loop’s closing brace (}). No further code from the current iteration of the loop is executed, and no further iterations of the loop are started.

Break in a Switch Statement
When used inside a switch statement, break causes the switch statement to end its execution immediately and to transfer control to the code after the switch statement’s closing brace (}).

ControlFlow
Functions

profile
iOS Developer

0개의 댓글