What is the importance of the weak and unowned in swift?

1

I've been studying swift for some time and now I'm looking to dig deeper into the language and IOS platform.

I wanted to know more concretely what the importance is and how to use the weak and unowned correctly.

    
asked by anonymous 16.08.2017 / 18:35

1 answer

1

These references have to do with ARC. In practice you are telling the compiler what type of reference is that variable so that it can avoid circular references.

Typically you have strong, weak and unowned.

  • Strong protects the pointer from being deallocated by the ARC. While something has a reference to a strong object type, it is not deallocated. This leads to retain cycles. You have two objects with strong references, none will be deallocated.
  • Weak Do the opposite. Do not protect the pointer from being dealt with. An example of this type of objects are the IBActions. Imagine if they were not the weak type. You would have several problems.
  • Unowned is equal to weak type but can not be optional. The weak type, when it is deallocated, gets the nil value. Unowned is used when you know it will never be nil.

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

    
27.08.2017 / 16:33