How to create a 2D NSArray or NSMutableArray in a C object?

I want to ask about objective question C. I want to create a 2D NSArray or NSMutableArray in a C. What should I do? The object stored in the array is NSString *. Thanks, very mcuh.

+3
source share
3 answers

This is certainly possible, but I think it's worth noting that NSArrays can only contain objects , not primitive types.

A way around this is to use a primitive wrapper type NSNumber.

NSMutableArray *outer = [[NSMutableArray alloc] init];

NSMutableArray *inner = [[NSMutableArray alloc] init];
[inner addObject:[NSNumber numberWithInt:someInt]];
[outer addObject:inner];
[inner release];

//do something with outer here...
//clean up
[outer release];
+5
source

NSMutableDictionary NSNumbers . , - .

"2D-"

NSMutableDictionary *twoDArray = [NSMutableDictionary dictionary];
for (int i = 0; i < 10; i++){
    [twoDArray setObject:arrayOfStrings forKey:[NSNumber numberWithInt:i]];
}

NSString *string = [[twoDArray objectForKey:[NSNumber numberWithInt:3]] objectAtIndex:5];
//will pull string from row 3 column 5 -> as an example
+1

, . , 2D-. , , , . , , , ( ) , . 2- . . , , . , .

, , 6x6:

int arrayStride=6;
int arrayDepth=6;
NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:arrayStride*arrayDepth];

, ;

for(int i=0; i<arrayStride*arrayDepth; i++) [newArray addObject @"whatever"];

Then after that you can access the objects using firstDim + secondDim * 6

int firstDim = 4;
int secondDim = 2;
NSString *nextString = [newArray objectAtIndex:firstDim+secondDim*6];
-1
source

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


All Articles