Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional unowned reference versus weak in Swift 5.0

This is allowed in Swift 5.0:

class Person { 
    unowned var child: Person?
}

This is supported by this release notes:

unowned and unowned(unsafe) variables now support Optional types. (47326769)

I understood exactly the difference between weak and unowned in Swift 4.2 and before. However, I am not sure why Apple decided to make the unowned an optional type. Even in the docs (which are docs for Swift 5.0) this implemented 'proposal' (where can I even find that proposal with the motivation to add optional unowned references?) isn't updated, because it says:

An unowned reference is expected to always have a value. As a result, ARC never sets an unowned reference’s value to nil, which means that unowned references are defined using non-optional types.

Above isn't true anymore. The only functional difference that Apple states is that an unowned reference is expected to have an equal or longer lifetime than the object holding that reference. Well, I am curious about the technical use of this.

What difference does it make when I use a weak reference vs an optional unowned reference? Or is the only difference that optional unowned should be used when the referencing object has a longer lifetime? I would expect there must be more...

like image 690
J. Doe Avatar asked Feb 24 '19 14:02

J. Doe


People also ask

What is the difference between unowned and weak reference?

Difference between weak reference and unowned reference 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 is difference between strong weak and unowned in Swift?

The key difference between a strong and a weak or unowned reference is that a strong reference prevents the class instance it points to from being deallocated. That is very important to understand and remember. ARC keeps track of the number of strong references to a class instance.

When should you use unowned or weak Swift?

According to Apple's docs: “Use a weak reference whenever it is valid for that reference to become nil at some point during its lifetime. Conversely, use an unowned reference when you know that the reference will never be nil once it has been set during initialisation.”

Are optional variables weak in Swift?

No, weak and optional are not the same, but there is some interplay between the two. Optional just means that a variable can be nil , either by assigning nil yourself, or becoming nil through some other means. The weak keyword has to do with memory management.

What is an unowned reference in Swift?

Unowned References in Swift Another solution to retain cycles is an unowned reference. Like a weak reference, an unowned reference does not increment or decrement the reference count of an object. However, unlike a weak reference, the program guarantees to the Swift compiler that an unowned reference will not be nil when it is accessed.

What is a weak reference in Swift?

Weak References are one solution to retain cycles in Swift. A weak reference does not increment or decrement the reference count of an object. Since weak references do not increment the reference count of an object, a weak reference can be nil. This is because the object could be deallocated while the weak reference is pointing to it.

When to use weak and when to use Unowned references?

Our focus will be on when to use weak and when it is better to use unowned by real-world examples. Define a capture in a closure as an unowned reference when the closure and the instance it captures will always refer to each other, and will always be deallocated at the same time.

What is the difference between strong reference and weak reference?

Contrary to strong reference, weak reference has no impact on an object’s reference count. Meaning that if we declare a weak variable pointing to an object, that object’s reference count will remain the same as it was before. Lets see what that means in practice with the following simple example:


1 Answers

You've misunderstood the release note and the meaning of the change in the language.

why Apple decided to make the unowned an optional type

They didn't. You can, and usually will, still say

unowned let owner : MyViewController

The only change here is that the unowned variable can be an Optional, which was illegal previously. This change takes care of an annoying edge case, that's all.

Above isn't true anymore

Yes, it is. Things are completely unchanged from before:

  • Weak references must be typed as Optional; they do not retain the object referred to, but they track the object referred to, and revert to nil if that object goes out of existence.
  • Unowned references do not retain the object referred to and do not track the object referred to, so it's up to you to prevent that object from going out of existence or you may end up with a dangling pointer and a crash.

The only thing that's changed is that there used to be an additional rule that an unowned reference type could not be an Optional. That rule is now gone.

As you rightly point out, if the unowned reference type is an Optional, this must be a var reference, not a let reference (because having this be an Optional would make no sense if you didn't have the power to change it from nil to an actual value and vice versa).

A typical use case is very much like what you yourself provided:

class Node {
    unowned var parent: Node?
}

It seems reasonable to say that this Node may or may not have a parent (because it might be at the top of the graph), but that if it does have a parent, that parent should be unowned (a parent should retain its child, but a child should not retain its parent). Previously, the only way to say that was to make this a weak reference, which entails some unnecessary overhead, and is otiose, because we can absolutely guarantee that if a node has a parent, the parent will outlive the child. Now, you can say what you mean, which is generally a good thing.

like image 117
matt Avatar answered Oct 20 '22 16:10

matt