Beyond NARC: when to save, when to assign, when to copy

Finally, I understand whether I should free the object and how to save as few delays in the trivial application as possible (knowing whether the object returns with an additional “save” already called or not). Therefore, NARC rules work very well in this regard.

On the other hand, I cannot wrap my head in retain(usually storing properties) or just instance variables assign. Are there any rules of thumb there? I only know two:

  • If I made an instance inside my class (e.g. in init), then I have to save it and release it to dealloc.
  • Delegates get assigned, not saved

But I do not understand if I should save the material that I transferred from another copy. I know that the risk is that the number of deductions is reduced to zero, and I leave the bag (or leave a lot of additional amounts and memory), but ... how can I organize my application so that this does not happen?

I saw some related questions , but nothing in common.

While I am here: What can I say about things that have been added to UIWindowor UIViewthat I personally do not save? If their supervisor is removed from all supervisors, will the "natural" instances be freed to be saved?

+3
source share
2 answers

. , , ? - , . , , , .

, ( ) .

, . - . , A B, B A. A B B A, dealloc, . , - . .

, node , . , , , ​​ . , , , .

, . , / . , ( ). " ".

, , , NSCopying, , . Cocoa - NSDictionary. , . NSDictionary .

, , , ( ), ... , t ?

: Cocoa . - , , . - , , .

+2

, :

  • retain
  • copy, /
  • assign,

, retain copy released.

(, init), dealloc.

, , , :

, - , . , .

+10

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


All Articles