iOS, Swift/Swift Theory
Swift ) Swift 5.7 변경사항 - EEYatHo iOS
EEYatHo
2023. 2. 6. 18:15
if let 약식 구문 등장
let foo: String? = "hello world"
// if let foo = foo {
if let foo {
print(foo)
}
any 확장
- any 키워드를 사용한 모듈을 만들 때,
type erase 로 type relationship 이 제거되어 associatedType 을 디스패치하지 못하던 상황을 개선
=> any 타입을 unboxing 하여 some 타입으로 컨버팅 할 수 있는 기능을 컴파일러에 추가
func feed(_ animal: some Animal) {
let food = type(of: animal).Feed.make() // associatedType 접근
animal.eat(food)
}
func feedAll(_ animals: [any Animal]) {
for animal in animals {
feed(animal) // some 타입으로 컨버팅
}
}
protocol Animal {
associatedtype Feed: AnimalFood
func eat(_ food: Food)
}
protocol AnimalFood {
static func make() -> Food
}
protocol Food {}
some 확장
- 프로퍼티 및 리턴 타입 뿐만 아니라, 파라미터 타입에도 some 키워드 사용 가능
func someFunc(_ v1: some Codable, _ v2: some Hashable) -> Int {...}
etc
https://github.com/apple/swift/blob/main/CHANGELOG.md#swift-57
- actor 변경사항
- 정규식 변경사항
- 시간을 나타내는 새로운 유형
- 클로저의 타입유추 범위 향상
- 제네릭 파라미터에 기본값 넣기 가능
- 최상위 스크립트에서의 await 허용 ( MainActor 에서 실행 )
Reference
Swift 5.7 Released!
Swift 5.7 is now officially released! Swift 5.7 includes major additions to the language and standard library, enhancements to the compiler for a better developer experience, improvements to tools in the Swift ecosystem including SourceKit-LSP and the Swif
www.swift.org
GitHub - apple/swift: The Swift Programming Language
The Swift Programming Language. Contribute to apple/swift development by creating an account on GitHub.
github.com
[Swift] some, any 키워드
WWDC 2022 > Embrace Swift Generics 에 나오는 내용입니다. Feed (사료, 먹이) 를 associatedtype으로 가지는 Animal 프로토콜을 예제로 사용합니다. [1] AS IS Farm에 feed라는 메소드를 만들고 싶을 때, 아래 두가지
eunjin3786.tistory.com
반응형