3

I'm struggling to create an array of ints in objective c.

the way I'd do this is javascript would be:

arr = new Array ( 1, 2, 3, 4, 3, 2, 2 );

I need to use an NSMutableArray, as the values need to be updated (it's a tile map for a game).

How can I do this?

Justin Boo
  • 10,132
  • 8
  • 50
  • 71
rob g
  • 113
  • 1
  • 4

4 Answers4

4

Does the array need to change length?

If not, why not use a normal array of ints:

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

Note that an NSArray (or subclass thereof) doesn't hold int types natively, you have to create an NSNumber object.

If you really need an Objective-C style array, then use this:

int vals[] = { 1, 2, 3, 4, 3, 2, 2};  // you still need this
int n = sizeof(vals) / sizeof(vals[0]);

[NSMutableArray* array = [[NSMutableArray alloc] initWithCapacity:n];
for (int i = 0; i < n; ++i) {
    [array addObject: [NSNumber numberWithInt:vals[i]]];
}
Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • the length does not need to change... but i need to be able to modify the array, can i do that? also how do i get the length of that array? [arr count] doesn't work – rob g Mar 15 '09 at 17:56
  • You might also use an NSArray and friends for a length, bounds checking, etc. – Jesse Rusak Mar 15 '09 at 17:57
  • the length of the array is sizeof(arr) / sizeof(arr[0]). This is the standard C mechanism for finding the length of a static array – Alnitak Mar 15 '09 at 17:57
2

For an NSMutableArray (though a C-style array may be preferable, as suggested by Alnitak), you need to do something like this:

NSMutableArray *array = [NSMutableArray arrayWithObjects: [NSNumber numberWithInt: 1], [NSNumber numberWithInt:2], nil];

(You can put as many as you want into that constructor, just separate them with commas and end with a nil.)

You could also make an empty array ([NSMutableArray array]) and then fill it with your ints using [array addObject:[NSNumber numberWithInt:1]].

If you need a 2D version, take a look here.

Community
  • 1
  • 1
Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
0

In addition to Alnitak's answer, I would add that this question also suggests some confusion that I certainly felt while learning to code iPhone programming. Here are a few tips in general about programming languages on the iPhone

  • Not all the code you write is objective-c.
  • You can (and often should) make use of other languages in programming, most notably C.
  • Alnitak's "normal array" is a C array. It is great for storing information with primitive data types.
  • If you want to store objects in your array, you use an NSArray. This is not a feature available in C. We know this immediately because it starts with "NS" (incidentally, this stands for Next Step).
bkbeachlabs
  • 2,121
  • 1
  • 22
  • 33
0

NSMutableArray can only contain Cocoa objects that inherit from NSObject (ie. Apple's classes and yours).

Basically for NSMutableArray you just init it like this:

myArray = [[NSMutableArray alloc] init];

and then add/delete objects as needed.

Good luck!

PS: Hank's class reference is a good starting place.

Genericrich
  • 4,611
  • 5
  • 36
  • 55