• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Newbie Memory Questions on a Code Fragment
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Newbie Memory Questions on a Code Fragment
      • From: Scott Ribe <email@hidden>
    • Re: Newbie Memory Questions on a Code Fragment
      • From: Kenneth Bruno II <email@hidden>
  • Prev by Date: Re: Setting the result of a javascript function to NSString
  • Next by Date: Re: Newbie Memory Questions on a Code Fragment
  • Previous by thread: Re: Setting the result of a javascript function to NSString
  • Next by thread: Re: Newbie Memory Questions on a Code Fragment
  • Index(es):
    • Date
    • Thread