NSDictionary递归循环

时间:2013-06-11 18:22:04

标签: ios objective-c nsdictionary

我正在使用以下函数来获取preferences.plist文件的所有键和值位于:/private/var/preferences/SystemConfiguration/preferences.plist

- (void)FindKeysAndValuesInPlist:(id)object forKeyNamed:(NSString *)keyName{
if ([object isKindOfClass:[NSDictionary class]])
{
      NSLog(@"%@",keyName);
    [object enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
        [self FindKeysAndValuesInPlist:value forKeyNamed:key];
    }];

}
else if ([object isKindOfClass:[NSArray class]])
{
    [object enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [self FindKeysAndValuesInPlist:obj forKeyNamed:nil];
    }];
}
else
{

    NSLog(@"%@.%@", keyName, object);
}   
}

preferences.plist的内容示例:

{
CurrentSet = "/Sets/3B9E7BEB-5558-4497-803B-21B03E6A46C0";
NetworkServices =     {
    "014226AB-75B7-41CF-9B96-48E82FD6A395" =         {
        Interface =             {
            DeviceName = ip4;
            Hardware = "com.apple.CommCenter";
            Type = "com.apple.CommCenter";
            UserDefinedName = "com.apple.CommCenter (ip4)";
        };
        PrimaryRank = Never;
        UserDefinedName = "com.apple.CommCenter (ip4)";
        "com.apple.CommCenter" =             {
            AllowNetworkAccess = 0;
            Available = 1;
            Version = 11;
        };
    };
    "1C01B561-1A55-4E3B-82FC-CDFF5024F0D2" =         {
        Interface =             {
            DeviceName = ip1;
            Hardware = "com.apple.CommCenter";
            Type = "com.apple.CommCenter";
            UserDefinedName = "com.apple.CommCenter (ip1)";
        };
        UserDefinedName = "com.apple.CommCenter (ip1)";
        "com.apple.CommCenter" =             {
            AllowNetworkAccess = 1;
            Available = 1;
            SettingsHaveBeenAlteredByPreferences = 1;
            Setup =                 {
                apn = "";
                password = "";
                signature = <7ecb277c ad546563 3ac057fb db40aeaa 939f8c0e e7ae68c2 6e0ff602 77d3868d 18a63059 6c83f66d 46b8af57 d1bf83d0 2655ced6 57d773f4 5c7e733e 923aaa07 39165357 a4ecf270 130276f0 59c7470e 0b61a631 dff04fd1 0bc80cb4 a0dc0a03 96a8ebf0 74c24cdb 84c38239 9f6f7f05 ee032982 8ed1b72d b531405b 09e35f5b>;
                "type-mask" = 0;
                username = "";
            };
            Version = 11;
        };
    };

问题是我正在尝试创建这种输出格式:MAINKEY.SUBKEY.VALUE

例如:

NetworkServices =     {
"014226AB-75B7-41CF-9B96-48E82FD6A395" =         {
    Interface =             {
        DeviceName = ip4;
        Hardware = "com.apple.CommCenter";
        Type = "com.apple.CommCenter";
        UserDefinedName = "com.apple.CommCenter (ip4)";
    };

将是:NetworkServices.014226AB-75B7-41CF-9B96-48E82FD6A395.Interface.Hardware.ip4

1 个答案:

答案 0 :(得分:4)

迭代字典中的键。当键的值是另一个字典时,将该键推入堆栈并递归字典值。当值是字符串或数字时,然后连接堆栈中的键并打印键路径和值。

代码:

+ (void)printDict:(NSDictionary *)dict keyStack:(NSArray *)keys
{
    if (keys == nil) {
        keys = [NSArray array];
    }

    for (id key in dict) {
        id value = dict[key];
        if ([value respondsToSelector:@selector(objectForKey:)]) {
            [self printDict:value keyStack:[keys arrayByAddingObject:key]];
        } else {
            NSLog(@"%@ = %@", [[keys arrayByAddingObject:key] componentsJoinedByString:@"."], value);
        }
    }
}