What is the importance of Weak and unowned in Swift?

Asked

Viewed 399 times

1

I have been studying Swift for some time and now I want to delve into the language and the IOS platform.

I wanted to know more concretely what is the importance and the correct way to use Weak and unowned.

1 answer

1

These references have to do with the ARC. In practice you’re telling the compiler what kind of reference is that variable so he can avoid circular references.

Typically you have Strong, Weak and unowned.

  • Strong protects the pointer from being placed by the ARC. As long as something has a reference to a type of Strong object, it is not set. This leads to retain Cycles. You have two objects with Strong references, none will be set.
  • Weak Does the opposite. Do not protect the pointer from being directed. An example of this type of objects are Ibactions. Imagine if they weren’t the Weak type. You’d have a lot of problems.
  • Unowned is equal to type Weak but cannot be optional. Type Weak, when it is dealocado gets value nil. Unowned is used when you know it will never be nil.

There is more theory behind this question, but this is the basic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.