728x90
알아두면 쓸모 있는 스위프트 잡지식
문자열 들여 쓰기
큰따옴표(") 3개를 쓰는 문자열에서 들여 쓰기의 기준은 닫는 따옴표 기준
let (apples, oranges) = (3, 5)
let quotation1 = """
Even though there's whitespace to the left,
the actual lines aren't indented.
Except for this line.
Double quotes (") can appear without being escaped.
I still have \(apples + oranges) pieces of fruit.
"""
//Even though there's whitespace to the left,
//the actual lines aren't indented.
// Except for this line.
//Double quotes (") can appear without being escaped.
//
//I still have 8 pieces of fruit.
let quotation2 = """
Even though there's whitespace to the left,
the actual lines aren't indented.
Except for this line.
Double quotes (") can appear without being escaped.
I still have \(apples + oranges) pieces of fruit.
"""
//insufficient indentation of line in multi-line string literal
// I still have \(apples + oranges) pieces of fruit.
// ^
let quotation3 = """
Even though there's whitespace to the left,
the actual lines aren't indented.
Except for this line.
Double quotes (") can appear without being escaped.
I still have \(apples + oranges) pieces of fruit.
"""
// Even though there's whitespace to the left,
// the actual lines aren't indented.
// Except for this line.
// Double quotes (") can appear without being escaped.
//
// I still have 8 pieces of fruit.
배열의 마지막 쉼표
배열에서 마지막에 쉼표가 허용된다.
let array1: [String] = ["a", "b", "c", ] // OK
let array2: [String] = [ // OK
"a",
"b",
"c",
]
switch-case let
스위치 문은 정수와 equality 외에도 비교를 지원한다.
let vegetable = "red pepper"
switch vegetable {
// ...
case let x where x.hasSuffix("pepper"):
print("Is it a spicy \(x)?")
// ...
}
함수는 1급 객체
함수가 다른 함수를 값으로 반환 가능
func makeIncrementer() -> ((Int) -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
var increment = makeIncrementer()
increment(7)
// 수행 순서
// 1단계: makeIncrementer 함수 호출
// makeIncrementer() 함수가 실행되고, addOne 함수 반환
//
// var increment = makeIncrementer()
// 이제 increment는 다음과 같은 함수로 동작
//
// func increment(_ number: Int) -> Int {
// return 1 + number
// }
// 2단계: increment(7) 호출
// increment(7)은 내부적으로 addOne(7)을 호출
//
// let result = increment(7)
// 3단계: addOne(7) 실행
// addOne 함수의 구현에 따라 7 + 1의 연산 수행
//
// func addOne(number: Int) -> Int {
// return 1 + number
// }
//
// 7이 number로 전달되고, 1 + 7 = 8 반환
// 결과: result = 8
다른 함수 중 하나를 인수로 가질 수 있다.
func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool {
for item in list {
if condition(item) {
return true
}
}
return false
}
func lessThanTen(number: Int) -> Bool {
return number < 10
}
var numbers = [20, 19, 7, 12]
hasAnyMatches(list: numbers, condition: lessThanTen)
클로저의 인수, 반환
클로저 내의 인수와 반환의 타입 명시 가능
numbers.map({ (number: Int) -> Int in
let result = 3 * number
return result
})
// @inlinable public func map<T, E>(_ transform: (Element) throws(E) -> T) throws(E) -> [T] where E : Error
do-catch 에러 타입 캐스팅
enum PrinterError: Error {
case outOfPaper
case noToner
case onFire
}
do {
let printerResponse = try send(job: 1440, toPrinter: "Gutenberg")
print(printerResponse)
} catch PrinterError.onFire {
print("I'll just put this over here, with the rest of the fire.")
} catch let printerError as PrinterError {
print("Printer error: \(printerError).")
} catch {
print(error)
}
참고
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour
728x90
'Swift' 카테고리의 다른 글
알쓸스잡 - 3 (0) | 2024.12.30 |
---|---|
알쓸스잡 - 2 (0) | 2024.12.26 |
SwiftUI + Combine MVVM 아키텍쳐 분석 - 7 (0) | 2024.11.11 |
SwiftUI + Combine MVVM 아키텍쳐 분석 - 6 (0) | 2024.11.11 |
SwiftUI + Combine MVVM 아키텍쳐 분석 - 5 (0) | 2024.11.11 |