Reputation:
I've created an object, and now I'm trying to create an array full of these objects. I've tried a few different things with no success.
How can I do this?
Upvotes: 1
Views: 2411
Reputation: 5666
You can use an NSArray
, take a look at Apple's documentation.
If you wanna add them incrementally consider using a mutable collection like an NSMutableArray
(here in the doc)
Upvotes: 1
Reputation: 6856
You can do it one of two ways, with NSArray
or NSMutableArray
.
id obj1, obj2, obj3;
// This creates a static array
NSArray *array = [NSArray arrayWithObjects: obj1, obj2, obj3, nil];
// This creates a dynamic array
NSMutableArray *mutarray = [NSMutableArray array];
[mutarray addObject:obj1];
[mutarray addObject:obj2];
[mutarray addObject:obj3];
Upvotes: 4
Reputation: 243146
NSMutableArray * arrayOfFoos = [NSMutableArray array];
for (int i = 0; i < 100; ++i) {
Foo * f = [[Foo alloc] init];
[arrayOfFoos addObject:f];
[f release];
}
Upvotes: 2