Unowned vs Weak

Fahad C H
5 min readApr 8, 2018

Weak and unowned are used to solve leaked memory and retain cycles.Both do not increase the retain count.

Having said that, we won’t use unowned as common as weak. Because I would say either we don’t want to mess up the functionality or we don’t want to think twice. Like others, I do have source code that doesn’t even have single unowned variable.

Recently, I have got a chance to learn more about these terms and guess, what i found, weak and unowned are very simple and easy to use as long as you know when and how to put them.So here, I will help you to find that when and how :)

Unowned

Important points that you should keep in mind when you want to use unowned

· You could use unowned when the other instance has same life time or longer lifetime.

Take an example of Customer and CreditCard. Here, CreditCard cannot exist without a customer. A CreditCard instance never outlives the Customer that it refers to, And we only create a CreditCard instance by passing Customer instance in the initialiser. So we can absolutely guarantee that this CreditCard cannot exist without the Customer.

when customer becomes nil, both the Customer and CreditCard get deallocated.

When used in closures, unowned references act like they are implicitly unwrapped optionals.so when you use them in closures you don’t have to optional chain them and you don’t have to unwrap them.

Weak

weak will nullify the pointer whenever the reference is deallocated but unowned won’t do that, so that may results in dangling pointer.

Because weak references need to allow their value to be changed to nil at runtime, they are always declared as variables and optional

When used in closures, weak references act like optionals.so when you use them in closures you have to optional chain them.

Have you ever wondered about what is mean by [weak self] or [unowned self] in closures. It is known as CaptureList

Well, the square brackets indicate of a capture list, which is just an array, with weak self being the only object inside. Being an array, a capture list can do more than just capture weak self. You can capture as many things as you’d like:

I hope this was useful and if you have any doubt or need clarification,I’d love to hear them!

Unowned vs Weak

Weak and unowned are used to solve leaked memory and retain cycles.Both do not increase the retain count.

Having said that, we won’t use unowned as common as weak. Because I would say either we don’t want to mess up the functionality or we don’t want to think twice. Like others, I do have source code that doesn’t even have single unowned variable.

Recently, I have got a chance to learn more about these terms and guess, what i found, weak and unowned are very simple and easy to use as long as you know when and how to put them.So here, I will help you to find that when and how :)

Unowned

Important points that you should keep in mind when you want to use unowned

· You could use unowned when the other instance has same life time or longer lifetime.

Take an example of Customer and CreditCard. Here, CreditCard cannot exist without a customer. A CreditCard instance never outlives the Customer that it refers to, And we only create a CreditCard instance by passing Customer instance in the initialiser. So we can absolutely guarantee that this CreditCard cannot exist without the Customer.

when customer becomes nil, both the Customer and CreditCard get deallocated.

When used in closures, unowned references act like they are implicitly unwrapped optionals.so when you use them in closures you don’t have to optional chain them and you don’t have to unwrap them.

Weak

weak will nullify the pointer whenever the reference is deallocated but unowned won’t do that, so that may results in dangling pointer.

Because weak references need to allow their value to be changed to nil at runtime, they are always declared as variables and optional

When used in closures, weak references act like optionals.so when you use them in closures you have to optional chain them.

Have you ever wondered about what is mean by [weak self] or [unowned self] in closures. It is known as CaptureList

Well, the square brackets indicate of a capture list, which is just an array, with weak self being the only object inside. Being an array, a capture list can do more than just capture weak self. You can capture as many things as you’d like:

I hope this was useful and if you have any doubt or need clarification,I’d love to hear them!

--

--

Fahad C H

iOS developer focused on quality and continuous delivery