Search code examples
objective-cxcodearraysnsarraynsnumber

NSArray or NSMutableArray for NSNumber Listing


Okay, so I want to do something like this:

int array[] = {1, 2, 3, 4, 5};

How would I go about that if I want to use NSArray and NSNumbers instead of ints?

**Note:

I do not want something like

NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject: ];

I need to be able to put them all in a single set separated by a comma. (Weird but it makes my program easier to handle in that format. I'm mimicking some java code where that is done a lot, so itl make it easier to follow the tutorial.)


Solution

  • If you want to add all the elements at once, and you're not going to change it, then us an NSArray. You can fill it like this:

    NSArray *array = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],[NSNumber numberWithInt:4],[NSNumber numberWithInt:5],nil];