Whats the Difference Between Unowned and Weak References?
TIL I finally learned when to use unowned as opposed to using weak. Differences Under the hood: unowned is essentially a force-unwrap of a weak capture, with all that it entails. Because of this using it slightly more dangerous. Crash danger: Accessing an unowned reference while itโs nil will cause a crash. Compilation error: Every weak reference, must be an optional property. Otherwise youโll get a compilation error: weak var delegate: DataEntryDelegate = DataHandler() // ERROR: 'weak' variable should have optional type 'DataEntryDelegate?' Call site: Unlike weak references, itโs not mandatory for unowned references to be an optional. This makes the call site cleaner i.e. you donโt have to do the optional unwrapping dance. Similarities Both donโt keep a strong hold on the instance it refers to. Donโt Use unowned for async/network operations. Because the object may become nil and then accessing it would crash! ...