Allow implicit self for weak self captures, after self is unwrapped
About 2 min
Allow implicit self for weak self captures, after self is unwrapped 관련
HACKING WITH SWIFT
What's new in Swift?
Allow implicit self for weak self captures, after self is unwrapped | Changes in Swift 5.8
Allow implicit self for weak self captures, after self is unwrapped
Available from Swift 5.8
SE-0365 (apple/swift-evolution
) takes another step towards letting us remove self
from closures by allowing an implicit self
in places where a weak self
capture has been unwrapped.
For example, in the code below we have a closure that captures self
weakly, but then unwraps self
immediately:
import Foundation
class TimerController {
var timer: Timer?
var fireCount = 0
init() {
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] timer in
guard let self else { return }
print("Timer has fired \(fireCount) times")
fireCount += 1
}
}
}
That code would not have compiled before Swift 5.8, because both instances of fireCount
in the closure would need to be written self.fireCount
.
Other Changes in Swift 5.8
Lift all limitations on variables in result builders | Changes in Swift 5.8
Lift all limitations on variables in result builders
Function back deployment | Changes in Swift 5.8
Function back deployment
Allow implicit self for weak self captures, after self is unwrapped | Changes in Swift 5.8
Allow implicit self for weak self captures, after self is unwrapped
Concise magic file names | Changes in Swift 5.8
Concise magic file names
Opening existential arguments to optional parameters | Changes in Swift 5.8
Opening existential arguments to optional parameters
Collection downcasts in cast patterns are now supported | Changes in Swift 5.8
Collection downcasts in cast patterns are now supported