Newbie Memory Questions on a Code Fragment
Newbie Memory Questions on a Code Fragment
- Subject: Newbie Memory Questions on a Code Fragment
- From: James Cicenia <email@hidden>
- Date: Wed, 21 Jan 2009 08:23:03 -0600
Hello -
I am new to iphone and objective-c etc.
Here is some code I wrote for the appdelegate to initialize some
dictionaries and arrays:
My main question has to do with memory and releasing it. In my app I
will need listOfMonthNames for my popups
and will also need dictionaryOfProduceTypes to populate other
dependent popups.
thanks
James
-(void)initializeArraysAndDictionaries {
if(!listOfMonthNames){
NSArray *listOfMonthNamesTmp = [[NSArray alloc] initWithObjects:
@"January
",@"February
",@"March
",@"April
",@"May
",@"June
",@"July",@"August",@"September",@"October",@"November",@"December"];
// enumerate over items
printf( "----static array\n" );
[listOfMonthNames arrayByAddingObjectsFromArray:listOfMonthNames2];
// free memory
[listOfMonthNamesTmp release];
}
if(!dictionaryOfProduceTypes){
// Create a distinct dictionary of types that hold an array of
subtypes per type.
// Types and subtypes are attributes of the ProduceItem object
dictionaryOfProduceTypes = [[NSMutableDictionary alloc] init];
NSEnumerator *en = [produceItems objectEnumerator];
ProduceItem *item = nil;
while (item = [en nextObject]) {
if(![dictionaryOfProduceTypes objectForKey:item.type]){
//Doesn't exist yet, so allocate and initialize
[dictionaryOfProduceTypes takeValue:[[NSMutableArray alloc] init]
forKey:item.type];
}
NSMutableArray *subtypes = [dictionaryOfProduceTypes
objectForKey:item.type];
//Get the array then iterate through to see if we need to add a new
subtype to be added
NSEnumerator *en2 = [subtypes objectEnumerator];
NSString *subtype = nil;
BOOL found = FALSE;
while (subtype = [en2 nextObject]) {
if([subtype compare:item.subtype]){
found = TRUE;
break;
}
}
if(!found){
[subtypes addObject:item.subtype];
}
[dictionaryOfProduceTypes takeValue:subtypes forKey:item.type];
[subtypes release];
}
[dictionaryOfProduceTypes release];
}
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden