Search code examples
iphonensarray

iPhone: how to NSArray content to NSDictionary


I want to add content of NSArray's to NSDictionary r NSMultDictionary.

I have NSDictionary(empty), and I have NSArray with content and I have to store NSArray content in NSDictionary.

How can I do this?


Solution

  • You can add content of NSArray to NSDictionary by using following code.

    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    for(int i=0; i<[Array count]; i++){
    [dic setObject:[Array objectAtIndex:i] forKey:[NSString stringWithFormat:@"%d",i]];
    }
    

    The dic is the dictionary, you can use that.

    Or else you can add whole array as one object in the dictionary as

    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:Array forKey:@"array"];