Apple's Swift Programming Language Guide mentions the capture specifiers unowned(safe)
and unowned(unsafe)
, in addition to weak
and unowned
.
I (think I) understand the differences between weak
and unowned
; but what is the difference between unowned(safe)
and unowned(unsafe)
? The guide doesn't say.
Please: Don't rely on simply stating an Objective-C equivalent.
From what I understand, although I can't find a definitive source from Apple,
unowned
can be broken into two flavors,safe
andunsafe
.A bare
unowned
isunowned(safe)
: it is a specially wrapped reference which will throw an exception when a dealloced instance is referenced.The special case is
unowned(unsafe)
: it is the Swift equivalent of Objective C's@property (assign)
or__unsafe_unretained
. It should not be used in a Swift program, because its purpose is to bridge to code written in Objective C.So, you will see
unowned(unsafe)
when looking at the import wrapper for Cocoa classes, but don't use it unless you have to, and you will know when you have to.Update
__unsafe_unretained
is a simple pointer. It will not know when the instance being pointed at has be dealloced, so when it's dereferenced, the underlying memory could be garbage.If you have a defect where a dealloced
__unsafe_unretained
variable is being used, you will see erratic behavior. Sometimes enough of that memory location is good enough so the code will run, sometimes it will have been partially overwritten so you will get very odd crashes, and sometimes that memory location will contain a new object so you will get unrecognized selector exceptions.Transitioning to ARC Release Notes
Variable is accessed when it was dellocated already with attribute:
unowned
unowned(unsafe)
Here is a quote from Apple Developer Forums:
unowned
vsunowned(safe)
vsunowned(unsafe)
A simple Definition. which would clear the confusion.
-- unowned attributes : If you try to access an unowned reference after the instance that it refers to is deallocated, your program will crash.
-- unowned(Unsafe) attributes: If you try to access an unsafe unowned reference after the instance that it refers to is deallocated, your program will try to access the memory location where the instance used to be, which is an unsafe operation. (no guarantee wether this would executes or crashes)