views:

46

answers:

1

Hello, i have a NSDictionary and i get objects and keys in 'id' type format , with the following code:

    NSDictionary *temp =[[NSDictionary alloc]initWithObjectsAndKeys:array1,@"array1",array2,@"array2",nil];
    NSInteger count = [temp count];
    id objects[count];
    id keys[count];
    [temp getObjects:objects andKeys:keys];

Where array1 and array2 are NSArrays. Is there a way to convert id objects[n] to a NSArray ? (kind of pointless in this example cause array1 and array2 are already there , but this would be helpful in many ways)

A: 

Yes. Your array objects is C array, with count items in it. NSArray has an initializer that does what you need:

 + (id)arrayWithObjects:(const id *)objects count:(NSUInteger)count

So you would do

 NSArray * myNewArray = [NSArray arrayWithObjects:objects count:count];

in this case.

Docs here.

quixoto
Well this works fine , but i mean a conversion like id objects[0] (which contains array1 values) back to the original NSArray [ i need it when parsing some raw data retrieved from a file stream , so it's a bit more complicated than example code i wrote ]
Kostas.N
What does "back to the original array" mean? objects[0] IS already one of the arrays, and objects[1] IS already the other one. You're just pulling the same ones back out of the dictionary. Note that the order is NOT guaranteed, since the dictionary is inherently unordered. Compare with the key strings if you want to know which is which.
quixoto
ah this comment is what I was looking for :)
Kostas.N