Need help understanding the specific alloc / release idiom in iOS / Objective-C programming

I am an experienced C / C ++ programmer starting to learn Objective-C development. I am currently looking at a UICatalog sample and came across another idiom example. I saw several places and never understood.

code:

ButtonsViewController *buttonsViewController = [[ButtonsViewController alloc] initWithNibName:@"ButtonsViewController" bundle:nil]; [self.menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys: NSLocalizedString(@"ButtonsTitle", @""), kTitleKey, NSLocalizedString(@"ButtonsExplain", @""), kExplainKey, buttonsViewController, kViewControllerKey, nil]]; [buttonsViewController release]; 

AFAIK, this selects and initializes the ButtonsViewController, creates an NSDictionary for the ButtonsViewController and adds a dictionary to the NSMutableArray called menuList (which is the MainViewController member variable where the above code is located), and then releases the ViewController buttons that it just created. Later, MainViewController uses a dictionary entry to switch views to buttonViewController when necessary.

My question is: why is the buttonViewController still valid after this code? It was isolated and released without "saving" between them. Does something in NSDictionary or NSMutableArray add implicit "persistence"? If so, should I somehow figure it out, or is it just one of those things that you have to read and remember?

+4
source share
4 answers

This is the standard procedure in Cocoa collection structures to save the objects that they contain. So yes, the mutable array saves the view controller.

This is always the case, unless otherwise specified in the documentation.

+4
source

So, although it is true that the object is valid afterwards because the collection stores it, I am not sure if it is useful to think about it. If you intend to use an object in this method after adding it to the collection, you should not release it until you are done with it.

A way to think about it:

1) I have a claim for ownership of this object that I created

2) Now the collection and I both own this object

3) Now only the collection does, and I should not use the object, because I do not have rights to it

and ignore the fact that you can technically leave without paying attention to 3 in this case.

This allows you to use the NARC (new-alloc-retain-copy) rule to think about what stores objects.

+4
source

As already mentioned, NSDictionary and NSArray send saved messages to added objects, and also remove the release for objects. The above code conceptually transfers the ownership of the buttonViewController to the dictionary. An example of how you can know this ... from the documentation:

-addObject: forKey:

Parameters: anObject - value for the key. The object receives a save message before being added to the dictionary. This value should not be nil.

-removeAllObjects

Discussion: each key and corresponding value object is sent a release message.

+3
source

Yes, containers, such as NSDictionary and NSMutableArray, save (or copy in the case of dictionary keys) the objects that you paste into them.

See "Collection Programming Topics" from Apple.

+1
source

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


All Articles