how to iterate nested dictionaries in objective-c iphone sdk

前端 未结 5 795
天命终不由人
天命终不由人 2021-01-13 11:35

Hi I have a json string converted unsing the JSON framework into a dictionary and I need to extract its content. How could I iterate to the nested dictionaries? I have alrea

5条回答
  •  Happy的楠姐
    2021-01-13 11:41

    You probably have to try recursive function.

    -(void)recurse:(NSDictionary *)dict counter: (int*) i parent:(NSString *) parent{
        for (NSString* key in [dict allKeys]) {
            id value = [dict objectForKey:key];
            NSLog(@"%@ -> %@", parent, key);
            if ([value isKindOfClass:[NSDictionary class]]) {
                i++;
                NSDictionary* newDict = (NSDictionary*)value;
                [self recurse:newDict counter:i parent:key];
                i--;
            } else {
                //Do smthg
            }
        }
    }
    

    this will list all keys and its level.

提交回复
热议问题