NSArray: add multiple objects with same value

Matt S. picture Matt S. · May 21, 2010 · Viewed 15.3k times · Source

How can I add multiple objects to my NSArray? Each object will have the same value.

Ex.

I want the value "SO" added to my array 10 times

Answer

RedBlueThing picture RedBlueThing · May 21, 2010

You can initialize the array with a set of objects:

NSString * blah = @"SO";
NSArray * items = [NSArray arrayWithObjects: blah, blah, nil];

or you can use a mutable array and add the objects later:

NSMutableArray * mutableItems = [[NSMutableArray new] autorelease];
for (int i = 0; i < 10; i++)
    [mutableItems addObject:blah];