How can I create an array of ints

I am trying to create an int array in a c object.

the way I would do this is javascript:

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

I need to use NSMutableArray, since the values ​​need to be updated (this is a tile map for the game).

How can i do this?

+3
source share
4 answers

Does the array need to change length?

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

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

Note that NSArray (or its subclass) does not support types intnatively; you need 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]]];
}
+4
source

NSMutableArray ( C-, Alnitak), - :

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

( , , , nil.)

([NSMutableArray array]), int [array addObject:[NSNumber numberWithInt:1]].

2D-, .

+2

NSMutableArray Cocoa , NSObject (.. Apple ).

NSMutableArray :

myArray = [[NSMutableArray alloc] init];

/ .

!

PS: Hank .

0

Alnitak, , , , , , iPhone. iPhone

  • , , objective-c.
  • ( ) , C.
  • Alnitak "normal array" - C. .
  • , NSArray. , C. , "NS" (, ).
0

Source: https://habr.com/ru/post/1704794/


All Articles