views:

134

answers:

1

Hello, I have a for loop as below which populates this data into an array.

I would like this to essentially populate 15 different array's, I have written some Pseudo code below. Is this achievable?

for (int y = 1; y < 16; y++) {
NSArray *array + y  = item 
}
A: 

I ALWAYS have this question and always wish there was a way (like there is in PHP's variable variables), but I'm pretty sure theres not... the only way to do it would to make an array OF arrays like so:

Assuming you mean you want to assign a value to an element of one of the arrays:

NSArray *arrays[15];
for (int i = 0; i < 15; i++){
 arrays[i] = [NSArray arrayWithObject:[NSNumber numberWithInt:i]];
}

NSLog(@"%@",[arrays[0] objectAtIndex:0]);

of course the NSNumber isn't what you want, I was just using it as an example...

Hope this was what you wanted.

P.S. you could also use NSMutableArray's arrayWithCapacity instead of using a C style array, but whatever you want.

NSMutableArray *arrays = [NSMutableArray arrayWithCapacity:14];
for (int i = 0; i < 15; i++){
 [arrays insertObject:[NSArray arrayWithObject:[NSNumber numberWithInt:i]] atIndex:i];
}

NSLog(@"%@",[[arrays objectAtIndex:0] objectAtIndex:0]);
micmoo
So essentially you have an array with arrays?
Lee Armstrong
I suppose you could have an array of dictionaries?
Lee Armstrong
OK, this works! However on the insertObject line I want to insert an already constructed array if that is possible? I have processed a string that is comma separated and this would work if I could just insert this array.
Lee Armstrong
NSMutableArray *playerArrayTempMain = [NSMutableArray arrayWithCapacity:15]; [playerArrayTempMain insertObject:[NSArray arrayWithObject:array] atIndex:y]; NSLog(@"%@",[[playerArrayTempMain objectAtIndex:0] objectAtIndex:1]);
Lee Armstrong