씬디의 블로그
[Swift] 삼항연산자 본문
Swift
삼항연산자는 앞은 true, 뒤는 false라고 생각하면 된다.
? = true, : = false
조건을 확인하고 true이면 앞에 있는 값을 반환하고
false이면 뒤에 있는 값을 반환한다.
삼항연산자는 언제 쓸까?
if welcome {
print("안녕하세요!")
} else {
print("누구세요?")
}
이렇게 if 문을 사용할 수도 있지만,
let welcome = true
print(welcome ? "안녕하세요!" : "누구세요?")
//print = 환영합니다!
삼항 연산자를 사용하면 조건에 따라 두 결과 중 하나를 선택할 수 있으며
간결하게 수행 할 수 있다
https://developer.apple.com/documentation/swift/operator-declarations
Operator Declarations | Apple Developer Documentation
Work with prefix, postfix, and infix operators.
developer.apple.com
'App > Swift 문법' 카테고리의 다른 글
[Swift] 고차함수 filter (0) | 2023.12.01 |
---|---|
[Swift] .swapAt 배열 자리 바꾸기 (1) | 2023.12.01 |
[Swift] Partial Range From 범위에 관해서 (1) | 2023.11.30 |
[Swift] 고차함수 map (0) | 2023.11.30 |
[Swift] 고차함수 reduce (2) | 2023.11.30 |