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

It is standard procedure in the Cocoa frameworks for collections to retain the objects they contain. So yes, the mutable array is retaining the view controller.

It is standard procedure in the Cocoa frameworks for collections to retain the objects they contain. So yes, the mutable array is retaining the view controller. This is always the case, unless the documentation says otherwise.

So, while it's true that the object is valid afterwards because the collection retains it, I'm not sure it's useful to think of it that way. If you intend to use the object in that method after adding it to the collection, you shouldn't release it until you're done with it. The way to think about it is this: 1) I have a claim of ownership of this object I created 2) Now the collection and I both have ownership of this object 3) Now just the collection does, and I shouldn't use the object because I don't have ownership of it and ignore the fact that you can technically get away with not paying attention to 3 in this case.

This allows you to keep using the NARC (new-alloc-retain-copy) rule for thinking about what retains objects.

1 I've switched over to thinking about ownerships rather than method names. If I directly own a collection, and it directly owns the objects I put in it, I treat that as my indirect ownership of all of the objects in the collection, without directly owning them at the same time. My argument for this is it's one fewer thing to remember to clean up; I welcome arguments against.

– Peter Hosey Jan 19 at 5:12 I would say that that model ends up being more complicated due to having to track which objects actually take ownership of things you hand them. Things that copy their args don't, weak collections don't, delegates don't, etc... these are all intuitive to you and me, but it's a lot concepts to learn for a new Cocoa programmer. The local ownership model "I own this while I use it" doesn't require thinking about other objects.

– Catfish_Man Jan 19 at 5:16 1 More succinctly, indirect ownership models promote coupling. – Catfish_Man Jan 19 at 5:20 @Catfish_Man +1 as a general rule, any time you go more than one method call deep to access an object, you've gone too far. As a general rule.

:) – Dave DeLong Jan 19 at 5:34 That would be the en.wikipedia. Org/wiki/Law_of_Demeter :) – Catfish_Man Jan 19 at 5:36.

As mentioned, NSDictionary and NSArray send retain messages to objects added and also send release to objects when removed. The code above is conceptually transferring ownership of buttonsViewController to the dictionary. An example of how you might know this... from the documentation : -addObject:forKey: Parameters: anObject - The value for key.

The object receives a retain message before being added to the dictionary. This value must not be nil. -removeAllObjects Discussion: Each key and corresponding value object is sent a release message.

Yes, containers like NSDictionary and NSMutableArray retain (or copy, in the case of dictionary keys) the objects you put into them. See the Collections Programming Topics guide from Apple.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions