how to iterate nested dictionaries in objective-c iphone sdk

前端 未结 5 783
天命终不由人
天命终不由人 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-13 11:48

    I have tried recursion and hope this could help.
    P.S. This will return the first occurrence of the key.

    +(id)getValue:(NSDictionary*)aDictionary forKey:(NSString *)aKey {
       id finalValue = nil;
       for (id key in [aDictionary allKeys]) {
          id value = aDictionary[key];
          if (value != nil && [key isEqualToString:aKey]) {
             finalValue = value;
             break;
          }
          else if ([value isKindOfClass:[NSDictionary class]]) {
           finalValue = [[self class] getValue:value forKey:aKey];
          }
       }
       return finalValue;
    }
    

提交回复
热议问题