objective c - Beyond NARC: When to retain, when to assign, when to copy -


I understand that I should issue an object, and know as little as possible in a trivial application By whether the item comes back with any additional 'retainer' or not), the rules of the NARC are working very well in that respect.

On the other side, I can not wrap my head around retaining (usually with retaining properties) or simply assign Example variables there are no rules of thumb? I only know about two:

  1. If I made an example in my class (for example, in an init), then I should maintain it and send it to dealloc < / Code>.
  2. Representatives are assigned, have not maintained

But what I do not understand is that I should retain that which I have another Examples have passed. I know that the risk is that the count of maintenance becomes zero and I have left the bag (or holds a lot of extra bags and memory), but ... how should I organize my application so that it does not ?

I have seen something, but nothing is normal.

When I'm Here: About the things that UIWindow or UIView is not individually created by me Am I If their supervisor is removed from all observers, will the "retrospective" examples be retracted?

To keep my maintenance matters as low as possible

< / Blockquote>

I do not understand this. Do you think you need to worry about maintaining it all? The calculation of maintaining is an implementation description, forget about it. You need to worry that you are the owner of something or not.

I keep my head around or not (usually to maintain properties) or just to provide the instance variable. >

In almost all cases you may want to keep or copy while assigning an example variable. The main exception cycle maintains Sometimes, you see yourself in a situation where the object A's object is the object B object reference and object B object is in the context. If A is retained and B has been retained, then you can not trust the general pattern of releasing references in Delac Get them away In this situation, nominating an object as the owner is normal and The second is owned by the ownership object then the owner will not make reference.

An example of this would be a tree structure where parents have references to their nodes of the node and each child has references to their parents if every child has kept their parents. The application is done with the parents, it will not go away, because it is maintained by all its children, hence the reference to the parents is applied as a weak reference, which means that Chche have to not be his parents.

This is representative of an object with the representatives of an object, which is representative of an object, for example, the controller of a table view is often the data source and / or representative of the table view. If the table view representative is maintained, then you have a retaining cycle (because the controller has already maintained the view). This is the reason that the conference "representatives have not been kept" has been generated.

You should definitely copy that if the object you want to reference needs to be NSCPeing and you do not need to change it when you are owned. An example from cocoa is the key to elements in an NSDictionary. If the key is to be converted into a dictionary, then it will be a disaster. Therefore NSDictionary copies its keys.

I know the risk is that the count of maintenance remains zero and I have left the bag (or holds a lot of extra bags and memory), but have left. .. How do I organize my application so that it does not?

Simple: Follow. If any thing goes away when you refer to it, then you were not the owner of that thing. If you need to be hanged longer than the current law, then you should keep it.


Comments