Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference in Swift between 'unowned(safe)' and 'unowned(unsafe)'?

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.

like image 225
orome Avatar asked Oct 24 '14 18:10

orome


People also ask

What is the difference between weak and unowned in Swift?

The weak reference is an optional type, which means weak reference will set to nil once the instance it refers to frees from memory. On the other hand, unowned reference is a non-optional type, it never will be set to nil and always have some value.

What does unowned mean in Swift?

Unowned references are similar to weak references in that they don't keep a strong reference to the instance they are referencing. They serve the same purpose as weak references, that is, they avoid strong reference cycles.

What is unowned in IOS?

An unowned reference presumes that it will never become nil during its lifetime. An unowned reference must be set during initialization - this means that the reference will be defined as a non-optional type that can be used safely without checks.

Can unowned be nil?

Unowned variables are similar to weak variables in that they provide a way to reference data without having ownership. However, weak variables can become nil – they are effectively optional.


1 Answers

From what I understand, although I can't find a definitive source from Apple, unowned can be broken into two flavors, safe and unsafe.

A bare unowned is unowned(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

__unsafe_unretained specifies a reference that does not keep the referenced object alive and is not set to nil when there are no strong references to the object. If the object it references is deallocated, the pointer is left dangling.

like image 179
Jeffery Thomas Avatar answered Oct 06 '22 00:10

Jeffery Thomas