앱 개발/iOS
Swift Notes for Professionals PDF (2) Booleans 요약
달모드
2024. 9. 19. 10:57
Section 5.2: Booleans and Inline Conditionals (불리언과 삼항연산자)
Boolean 값을 가지는 인자 value를 함수 내부에서 삼항연산자로 받아서 이것을 String안에 나타내는~
func isTurtle(_ value: Bool) {
let color = value ? "green" : "red"
print("The animal is \(color)")
}
isTurtle(true) // outputs 'The animal is green'
isTurtle(false) // outputs 'The animal is red'
Section 5.3: Boolean Logical Operators
OR ( || ), AND ( && ), XOR ( ^ )
XOR 연산자는 두 operands 중에서 하나만 true값을 가져도 true 값을 반환하는 것이다.
if (10 < 20) ^ (20 < 10) {
print("Expression is true")
}
// Expression is true
Section 5.4: Negate a Bool with the prefix ! operator
!는 logical negation(논리적 부정)을 담당하는 연산자이다.
print(!true) // prints "false"
print(!false) // prints "true"
func test(_ someBoolean: Bool) {
if !someBoolean {
print("someBoolean is false")
}
}