December 2018
Intermediate to advanced
414 pages
10h 19m
English
Swift provides us with two keywords that indicate how we want to extend the lifetime of an object in a closure. While both prevent creating retain cycles, they are fundamentally different.
Using weak will wrap the captured value inside of an optional, indicating that the instance may have been deallocated before the closure was executed:
class MyClass { var running = false func run() { running = true DispatchQueue.main.asyncAfter(deadline: .now() + 10) { [weak self] in self?.running = false } }}var instance: MyClass? = MyClass()instance?.run()instance = nil
In this execution, instance will immediately be deallocated when set to nil.
Using unowned indicates that the variable won't be owned by the block. Another mechanism ...
Read now
Unlock full access