self is no longer required in many places
About 2 min
self is no longer required in many places 관련
HACKING WITH SWIFT
What's new in Swift?
self is no longer required in many places | Changes in Swift 5.3
self is no longer required in many places
Available from Swift 5.3
SE-0269 (apple/swift-evolution
) allows us to stop using self
in many places where it isn’t necessary. Prior to this change, we’d need to write self.
in any closure that referenced self
so we were making our capture semantics explicit, however often it was the case that our closure could not result in a reference cycle, meaning that the self
was just clutter.
For example, before this change we would write code like this:
struct OldContentView: View {
var body: some View {
List(1..<5) { number in
self.cell(for: number)
}
}
func cell(for number: Int) -> some View {
Text("Cell \(number)")
}
}
That call to self.cell(for:)
cannot cause a reference cycle, because it’s being used inside a struct. Thanks to SE-0269, we can now write the same code like this:
struct NewContentView: View {
var body: some View {
List(1..<5) { number in
cell(for: number)
}
}
func cell(for number: Int) -> some View {
Text("Cell \(number)")
}
}
This is likely to be extremely popular in any framework that makes heavy use of closures, including SwiftUI and Combine.
Other Changes in Swift 5.3
Multi-pattern catch clauses | Changes in Swift 5.3
Multi-pattern catch clauses
Multiple trailing closures | Changes in Swift 5.3
Multiple trailing closures
Synthesized Comparable conformance for enums | Changes in Swift 5.3
Synthesized Comparable conformance for enums
Type-based program entry points | Changes in Swift 5.3
Type-based program entry points
where clauses on contextually generic declarations | Changes in Swift 5.3
where clauses on contextually generic declarations
Enum cases as protocol witnesses | Changes in Swift 5.3
Enum cases as protocol witnesses
Refined didSet semantics | Changes in Swift 5.3
Refined didSet semantics
A new Float16 type | Changes in Swift 5.3
A new Float16 type
Swift Package Manager gains binary dependencies, resources, and more | Changes in Swift 5.3
Swift Package Manager gains binary dependencies, resources, and more