C-Style Arrays of Objective-C objects

 
Post new topic   Reply to topic    Mulle kybernetiK Optimization Forum Index :: Foundation & Objective-C
View previous topic :: View next topic  
Author Message
scottstevenson



Joined: 05 Sep 2004
Posts: 1
Location: Sunnyvale, CA

PostPosted: Sun Sep 05, 2004 9:55 pm    Post subject: C-Style Arrays of Objective-C objects Reply with quote

A simple one, but I think sometimes people forget that you're free to use c-style arrays of Objective-C objects where you need ever last ounce of speed:

Code:

// choose arbitrary size for array

int count = 20;

// allocate memory for array of pointers

NSString ** stringArray = (NSString **)malloc ( sizeof ( NSString *) * count );

// create objects and log them to the console

for (i = 0; i < count; i++)
{
    stringArray[i] = [[NSString alloc] initWithFormat:@"String %d",(i+1)];
    NSLog(@"stringArray[%d]: %@", i, stringArray[i]);
}

// release objects

NSString * theString;
for (i = 0; i < count; i++)
{
    theString = stringArray[i];
    [theString release];
}   

// free malloc'd memory

free (stringArray);




Also worth looking at NSArray's -getObjects:

- Scott
_________________
--
http://cocoadevcentral.com/
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Mulle kybernetiK Optimization Forum Index :: Foundation & Objective-C All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Powered by phpBB © 2001, 2002 phpBB Group
Charcoal2 Theme © Zarron Media