I have code in my project as follows. I am adding view controllers' view to the each cell of table view.
An array, like these
NSMutableArray *arrTbl=[[NSMutableArray alloc] init];
NSMutableDictionary *dOne=[[NSMutableDictionary alloc] init];
myViewController *objVCtr=[[myViewController alloc] init开发者_开发问答WithNibName:@"myViewController" bundle:nil];
[dOne setValue:objVCtr forKey:@"cellVCtr"];
NSMutableDictionary *dTwo=[[NSMutableDictionary alloc] init];
myViewController *objVCtr1=[[myViewController alloc] initWithNibName:@"myViewController" bundle:nil];
[dOne setValue:objVCtr1 forKey:@"cellVCtr"];
NSMutableDictionary *dThree=[[NSMutableDictionary alloc] init];
myViewController *objVCtr2=[[myViewController alloc] initWithNibName:@"myViewController" bundle:nil];
[dOne setValue:objVCtr2 forKey:@"cellVCtr"];
[arrTbl addObjectsFromArray:[NSArray arrayWithObjects:dOne,dTwo,dThree,nil]];
Now, the question is, how to release this?
arrTbl is main array having dictionaries & dictionaries having ref of view controllers.
So, Should I write following statements after above statements?
[dOne release];
[dTwo release];
[dThree release];
[objVCtr release];
[objVCtr1 release];
[objVCtr2 release];
After writing above code, array can point to the view controllers or not?
To summarise, the questions are:
- What does actually dictionary contain? (Is it retain count of View controllers or just reference of view controllers)
- What does array actually contain? (Is it retain count of dictionaries or just reference of dictionaries?)
- here I just want to have an array of view controllers & each view controllers with different values (for that I am adding dictionary to an array)
Dictionaries and arrays contain pointers to objects, but the internal implementation of these classes is not important. What you do need to pay attention to is the ownership of objects.
Have a look at http://boredzo.org/cocoa-intro/, in particular the memory management section which states:
- If you create an object with a method whose selector contains the word “alloc” or “new”, or with a function whose name contains the word “Create”, then you own it.
- If you make a copy of an existing object using a method whose selector contains the word “copy”, or from a function whose name contains the word “Copy” (or obtain an object from such a function), then you own the copy.
- Otherwise, you don't own the object.
- If you own an object, you are obliged to release it.
In your case you are creating the objects using -alloc
, so you own the objects and are responsible for them. So, yes, you do need to release them after adding them to the array.
NSArray
and NSDictionary
retain their members, so once you've added an object to an array or dictionary you can safely release it, the object won't be deallocated until the array itself removes the object or is deallocated itself.
To make it easier for yourself, you can use the convenience constructors which return autoreleased objects:
//note the autorelease when the view controllers are created
MyViewController* viewController = [[[MyViewController alloc] initWithNibName:@"myViewController" bundle:nil] autorelease];
NSDictionary *dOne = [NSDictionary dictionaryWithObject:viewController forKey:@"cellVCtr"];
MyViewController *objVCtr1 = [[[MyViewController alloc] initWithNibName:@"myViewController" bundle:nil] autorelease];
NSDictionary* dTwo = [NSDictionary dictionaryWithObject:objVCtr1 forKey:@"cellVCtr"];
MyViewController *objVCtr2 = [[MyViewController alloc] initWithNibName:@"myViewController" bundle:nil];
NSDictionary* dThree = [NSDictionary dictionaryWithObject:objVCtr2 forKey:@"cellVCtr"];
NSArray* arrTbl = [NSArray arrayWithObjects:dOne, dTwo, dThree, nil];
//do something with arrTbl
//make sure you retain it if you want to hang on to it
When adding objects to collections (such as dictionaries and arrays) the collection will retain the object being added.
If you want the object to live only as long as it is in the collection, a good habit is to either auto release an object before adding it to a collection or explicitly release an object just after adding it to a collection, like so:
MyObject *anObject = [[[MyObject alloc] init] autorelease];
[aDictionary setObject:anObject forKey:@"aKey"];
or
MyObject *anObject = [[MyObject alloc] init];
[aDictionary setObject:anObject forKey:@"aKey"];
[anObject release];
Remember that when an object is removed from the collection, the collection will release it.
This means that if the collection is the only retainer of the object, then the object will be deallocated after being removed from the collection.
精彩评论