이 글은 https://bbiguduk.gitbook.io/swift 에 나오는 스위프트 문법을 공부하면서 새롭게 알게 된 지식들을 정리한 포스팅입니다.
for-in
루프를 돌기 위해서는 해당 콜랙션은 시퀀스(Sequence
) 프로토콜을 준수해야 한다._
로 대체할 수 있다.let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
}
print(answer) // 59049
stride(from:to:by:)
: 반열림 범위 연산자(..<
)와 같은 기능이되 간격을 설정할 수 있다.stride(from:through:by:)
: 닫힌 범위 연산자(...
)와 같은 기능이되 간격을 설정할 수 있다.let minuits = 60
let hours = 12
let hoursInterval = 3
for tick in stride(from: 0, to: minuits, by: 5) {
print(tick) // 0 5 10 ... 45 50 55
}
for tick in stride(from: 0, through: hours, by: hoursInterval) {
print(tick) // 3 6 9 12
}
switch
구문은 여러 패턴 중 첫번째로 일치하는 case에 해당하는 코드 블럭을 실행한다.switch
는 break
없이도 해당 case
끝나면 switch
빠져나간다.case
는 반드시 하나 이상의 실행가능한 구문이 포함되어야 한다.,
로 구분하여 하나의 case
로 결합한다.let lowerCaseA: Character = "a"
switch lowerCaseA {
// case "a": // 에러
// case "A":
case "a", "A":
print("This is letter A")
default:
print("This is not A")
}
let num: Int = 50
switch num {
case ..<50:
print("less than 50")
case 50..<100:
print("less than 100")
case 100...:
print("more than 100")
default:
print("??")
}
switch
의 case
로 튜플을 사용할 수 있으며 각 요소는 값 또는 범위가 될 수 있다._
로 어떤 값도 일치하는 와일드카드 패턴을 사용할 수 있다.(0, 0)
은 모든 케이스에 해당되지만 첫번째 일치하는 case
만 사용된다.let somePoint: (Int, Int) = (1, 1)
switch somePoint {
case (0, 0):
print("origin")
case (_, 0):
print("x-axis")
case (0, _):
print("y-axis")
case (-2...2, -2...2):
print("inside of box")
default:
print("outside of bax")
}
case
에 일치하는 값을 상수나 변수에 할당하여 구문 내에서 사용할 수 있다.case
가 가능한 남아있는 모든 경우와 일치하기 때문에 default
가 필요없다.let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
print("\(x) on the x-axis")
case (0, var y):
print("\(y) on the y-axis")
case let (x, y):
print("\(x) and \(y)")
}
case
의 추가 조건으로 where
절을 사용할 수 있다.where
절의 조건 안에 들어 갈 상수/변수가 이미 선언되어 있어야 한다.let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
print("(\(x), \(y)) is on the line x = y")
case let (x, y) where x == -y:
print("(\(x), \(y)) is on the line x = -y")
case let (x, y):
print("(\(x), \(y) not on a line")
}
,
를 구분자로 하여 여러 개의 패턴을 하나의 단일 케이스로 결합 가능하다.case
에서 튜플 내 서로 다른 값이 바인딩될 때, 다른 타입이면 에러 발생한다.let stillAnotherPoint = (9, 0) // (Int, String) 등은 불가.
switch stillAnotherPoint {
case (let distance, 0), (0, let distance):
print("On an axis, and \(distance) from origin")
default:
print("Not on an axis")
}
switch
구문에서 의도적으로 case
를 무시하기 위해서 break
구문을 사용한다.switch
구문에서 해당 케이스 본문을 실행한 후 다음 케이스로 넘어갈 때 falltrough
사용한다.fallthrough
는 다음 케이스의 조건을 확인하지 않고 (default
가 아니어도) 무조건 실행한다.let number: Int = 5
var result: String = "This is a number of "
switch number {
case 2, 3, 5, 7, 11, 13, 17, 19:
result += "prime number and "
fallthrough
//case 6:
// result += "??"
default:
result += "integer"
}
print(result) // This is a number of prime number and integer
// 주석 지우면: This is a number of prime number and ??
if
, switch
)이나 루프 구문(for
, while
)에 구문 라벨을 붙일 수 있다.break
사용 가능 / 루프 구문: break
, continue
사용 가능break
는 라벨을 써야되지만, continue
는 필수는 아니다. (가독성 위해 권장)gameLoop: while square != finalSquare {
dice += 1
if dice == 7 { dice = 1 }
switch square + dice {
case finalSquare:
break gameLoop // 라벨을 쓰지 않으면 switch가 break 된다.
case let newSquare where newSquare > finalSquare:
continue gameLoop // 라벨을 쓰지 않아도 while이 continue되지만, 가독성 위해 쓴다.
default:
square += dice
square += board[square]
print(square)
}
}
print("END!")
guard
구문은 항상 else
절을 가지며, else
절의 코드는 조건이 거짓일 경우에만 실행된다.else
블럭으로 래핑하지 않아도 되기에 if
보다 선호된다.guard
구문은 주로 옵셔널 바인딩에 사용한다.guard
스코프(중괄호) 바깥에서만 사용가능하다.func greet(person: [String: String]) {
// guard person.count > 1 else {
// print("You gave less information..")
// return
// }
guard let name = person["name"] else {
return
}
print("Hello \(name)!")
guard let location = person["location"] else {
print("You're Homeless.")
return
}
print("You live in \(location).")
}
greet(person: ["name": "Yohan"])
defer
블럭 안의 코드는 현재 스코프의 마지막 순서에 실행된다.for
루프의 break
, 에러 throw
등도 포함된다.defer
블럭이 여러 개일 경우는 역순으로 실행된다.var score: Int = 3
if score < 100 {
score += 100
defer {
score -= 100 // 4: -50
}
defer {
score += 50 // 3: 50
}
defer {
score *= 0 // 2: 0
}
print(score) // 1: 103
}
print(score)) // -50
if
/guard
구문으로 확인할 수 있다.#available
#unavailable
을 사용한다.*
마지막에 입력.@available(macOS 10.12, *)
struct ColorPreference {
var bestColor = "blue"
}
func chooseBestColor() -> String {
guard #available(macOS 10.12, *) else {
return "gray"
}
let colors = ColorPreference()
return colors.bestColor
}
print(chooseBestColor()) // blue