Choosing Capture Modifiers
Prefer weak capture to unowned. Weak captures can be checked at the start of a closure and, if still valid, assigned to a strong reference for the life of the enclosing scope. They provide excellent safety and utility.
An unowned capture is equivalent to Objective-C’s unsafe-unretained; it can be used when “the closure and the instance it captures will always refer to each other, and will always be deallocated at the same time.” You may use unowned items to refer to global instances whose lifetime extends throughout the application’s duration.
An unowned reference guarantees that a reference always has a value. If there is any chance whatsoever that the unowned item may deallocate, using unowned items is roughly equivalent ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access