开发者

'id' type object to NSArray problem

开发者 https://www.devze.com 2023-01-14 16:51 出处:网络
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\"

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 ke开发者_Python百科ys[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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消