在NSDictionary中查找子键

时间:2015-01-22 20:28:21

标签: ios objective-c soap nsdictionary

我有一个很大的NSDictionary。 Fx的。

"m:GetFolderResponse" =     {
    "m:ResponseMessages" =         {
        "m:GetFolderResponseMessage" =             {
            ResponseClass = Success;
            "m:Folders" =                 {
                "t:CalendarFolder" =                     {
                    "t:ChildFolderCount" =                         {
                        text = 0;
                    };
                    "t:DisplayName" =                         {
                        text = Calendar;
                    };
                    "t:FolderId" =                         {
                        ChangeKey = "AgAAABYAAABGewbOYWpKSrW/k23iIoFkAPJWd7/8";
                        Id = "AAMkADkwOWE2NjEyLTMwZWQtNGYyMy05OGQ1LWZjZjFkZGY5MTBhMAAuAAAAAAC1cjo8jkv5SKjQt5WaSmd1AQBGewbOYWpKSrW/k23iIoFkAPJWc0NrAAA=";
                    };
                };
            };
            "m:ResponseCode" =                 {
                text = NoError;
            };
        };
    };
    "xmlns:m" = "http://schemas.microsoft.com/exchange/services/2006/messages";
    "xmlns:t" = "http://schemas.microsoft.com/exchange/services/2006/types";
};

}

正如您可能已经猜到的那样,m:文件夹中可能有多个。因此我想找到m:Folders child,其中t:DisplayName等于变量值。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

- (void)filterMutableDictionary:(NSDictionary*)aDictionary andKeyName:(NSString *)keyName
{
    if ([keyName isEqualToString:@"t:CalendarFolder"]) {
        if ([[[aDictionary objectForKey:@"t:DisplayName"] objectForKey:@"text"] isEqualToString:searchCalendarName]) {

            NSDictionary *temp = [aDictionary objectForKey:@"t:FolderId"];
            CalID = [temp objectForKey:@"Id"];
            CalChangeID = [temp objectForKey:@"ChangeKey"];

        }
    }

    // enumerate key/values, filtering appropriately, recursing as necessary
    NSLog(@"%@",aDictionary);

    [aDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
        if ([value isKindOfClass: [NSMutableDictionary class]] || [value isKindOfClass: [NSDictionary class]]) {
            [self filterMutableDictionary: value andKeyName:key];
        }
    }];
}