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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to