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] initWithNibName:@"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:
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.NSArrayandNSDictionaryretain 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: