1️⃣ 부울
var isAnimating = true
let gameover = false
let isMultiple = 120.isMultiple(of: 3)
- 참 또는 거짓을 나타낸다.
- bool 값을 받는다.
잘못된 경우
var isAnimating = 1
위 코드는 정수를 저장하므로, 부울을 생성하지 않는다.
값을 뒤집기
//방법 1
var isAuthenticated = false
isAuthenticated = !isAuthenticated //true 로 값 전환
//방법 2
var gameOver = false
gameOver.toggle() //true 로 값 전환
! 과 toggle() 을 이용해
값을 true에서 false로,
false에서 true로 뒤집을 수 있다.
2️⃣ 문자열 결합
let firstPart = "Hello, "
let secondPart = "world!"
let greeting = firstPart + secondPart // Hello, world
let luggageCode = "1" + "2" + "3" + "4" + "5" // 12345
+ 를 이용해 문자열을 결합할 수 있다.
잘못된 경우
//잘못된 경우!
let number = 11
let missionMessage = "Apollo " + number + " landed on the moon."
+는 문자열에 문자열을, 정수에 정수를, 십진수에 십진수를 더할 수 있지만
문자열에 정수를 더할 수 없다.
let missionMessage = "Apollo \(number) landed on the moon."
\( ) 를 통해서 원하는 변수, 또는 값을 동적으로 넣을 수 있다.
'Swift' 카테고리의 다른 글
[Day 4] Type annotations (0) | 2023.08.03 |
---|---|
[Day 3] 배열(Array) , 딕셔너리(Dictionary) , 집합(Set), Enum (0) | 2023.08.02 |
[Day 1] 변수와 상수, 문자열, 숫자 (0) | 2023.07.31 |
[XCODE] MARK 사용하기 (0) | 2023.07.30 |
[SwiftUI] AppStorage란? (0) | 2023.07.28 |