ARC and Interface Builder

I have an interesting question about interface builder and ARC. I am creating a view controller that will be initialized by xib. I am using the drag and drop function of the interface constructor to create the necessary outputs for my xib. This entails the property as (weak, non-atomic). Sounds great to me.

What throws me away is that strings like [self setCategoryButton:nil]; are automatically added to viewDidUnload. I understand and used this before ARC. However, since they are weak links, will they not automatically clear when the view is offloaded?

I believe that I could see the value zero of these properties if these representations are stored by other objects, but for me this seems an unusual case ... I just wanted to get more information about the point.

Thanks,

Kurt

+4
source share
4 answers

I filed an error for this some time ago, and the answer I received was that this was deliberate behavior. Since Xcode cannot verify that there are no other strong references to the view, it cannot actually guarantee that a weak pointer will be automatically cleared in -viewDidUnload . In this way, they cleanse it for you.

Personally, I think this is a lame reason, but the reason it was given.

+4
source

It will be installed only in nil in iOS5 or later (iOS 4 does not automatically contain weak pointers), so it exists for security in automatically generated code. I was curious about this, and this is the only reason I can come up with.

+3
source

From the document, viewDidUnload

  • "This method ... is your chance to do a final cleanup ..."
  • "When a low memory condition occurs and the views of the current dispatcher view are not needed, the system may choose to remove these views from memory."

From the documentation, about the weak (semantics of the sample)

  • Indicates that there is a weak (non-owning) connection with the target. If the target is freed, the property value is automatically set to nil .

So this is obvious. It does not apply to the object that your property points to, but simply nullifies your property based on the above rule for the weak.

ViewDidUnload assumes that your weak property cannot point to any object because this object is freed (depending on where viewDidUnload is in the View Controller life cycle). And just cleans it with zero.

This is safe for you, evident to weak rule and memory efficiency. The compiler cannot be sure that you have taken care of the pointed object. He just needs to provide cleaning.

+3
source

Since the property is weak , the link in your object does not affect the account of saving the object. So yes, it makes no sense to set the object to zero. At least as I understand it.

0
source

Source: https://habr.com/ru/post/1402315/


All Articles