将嵌套Plist转换为NSDictionaries&数组

时间:2012-04-21 21:12:26

标签: iphone nested nsdictionary property-list

鉴于此P-List词典:

enter image description here

我如何到达第3名。键 - “晚餐” - 它本身也是一个词典,并正确解析其值?

或者,我是否应该以不同的方式构建此P-List,以便我可以更轻松地获取所有内容?

这是我得到的,首先从我的'MenuDictionary'抓取所有键并将它们存储在一个数组中:

// Load the Property-List file into the Dictionary:
MenuDictionary = [[NSDictionary alloc] initWithContentsOfFile:menuPath];

// Get all the Keys from the Dictionary, put 'em into a 'mealTimesArray':
mealTimesArray = [[MenuDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];

// For each meal-type KEY, grab all the Values (dishes) in it and store them in a 'mealDishesArray':
for (NSString *mealTime in mealTimesArray) {
    NSArray *mealDishesArray = [MenuDictionary valueForKey:mealTime];

    // Now I can iterate through the 'mealDishesArray' to access each dish at
    // a time, so I can print them out or do whatever else:
    for (NSString *dish in mealDishesArray) {
        NSLog(@"Iterating through 'mealDishesArray' now...");
        NSLog(@"Current 'dish' is: %@", dish);

当我到达“Dinner”键时出现问题:它是一个包含2个具有2个数组值的键的字典。那么如何将其内容加载到Dictionary对象中呢?更具体地说,我应该使用什么'init'方法将“Dinner”内容加载到我的新Dictionary对象中?

我试过这个 - 不起作用:

// I put this inside the first fast-enum loop:

if ([mealTime isEqualToString: @"Dinner"]) {
   // init new Dictionary object (declared previously):
   dinnerDictionary = [[NSDictionary alloc] initWith ???];

我想用“Dinner”键的内容初始化它,但它显然不是P-List文件,所以我不能使用

   initWithContentsOfFile: pathName

我不明白哪个其他init方法可以让我访问“Dinner”的键和值。因为即使“Dinner”结构为字典,它当前也位于一个数组中,不会将其视为字典(我认为......)

我显然对此有点不清楚。

或者,我是否应该以不同的方式构建我的P-List,以便我可以获得这个嵌套的Dinner字典?

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我认为plist结构是有道理的,并且基于类有条件地处理内容也是完全可以的。我会在合理的期望范围内对plist中的内容作出反应,所以......

// for each meal...
for (NSString *mealTime in mealTimesArray) {
    // we're not sure what kind of meal we have
    id mealInfo = [MenuDictionary valueForKey:mealTime];

    if ([id isKindOfClass:[NSArray self]]) {
        // it's an array? cast as an array and deal with the array
        NSArray *mealDishesArray = (NSArray *)mealInfo;
        [self handleMealArray:mealDishesArray];
    } else if ([id isKindOfClass:[NSDictionary self]]) {
        // it's a dictionary?  that's cool, too. cast as a dictionary and deal with it
        NSDictionary *mealDictionary = (NSDictionary *)mealInfo;
        [self handleMealDictionary:mealDictionary];
    }
}

// you've worked out to handle the array
- (void)handleMealArray:(NSArray *)mealDishesArray {
    for (NSString *dish in mealDishesArray) {
        NSLog(@"Iterating through 'mealDishesArray' now...");
        NSLog(@"Current 'dish' is: %@", dish);
    }
}

// handle the dictionary like a dictionary, realizing that it contains
// arrays, which you've already worked out how to handle
- (void)handleMealDictionary:(NSDictionary *)mealDictionary {
    for (NSString *dishType in [mealDictionary allKeys]) {
        NSArray *mealDishesArray = [mealDictionary valueForKey:dishType];
        [self handleMealArray:mealDishesArray];
    }
}

答案 1 :(得分:0)

问题在于这一行:

NSArray *mealDishesArray = [MenuDictionary valueForKey:mealTime];

当mealTime为'Dinner'时,您将分配mealDishesArray值为NSDictionary。认为你有一个数组然后使用:

for (NSString *dish in mealDishesArray)

迭代数组中的元素,这些元素无法满足您对“晚餐”的期望。您可以考虑添加以下内容:

NSAssert ([mailDishesArray isKindOfClass: [NSArray class]], @"Expecting an array");

在您分配到mealDishesArray之后。

解决方案是什么?您的PLIST在“早餐”,“午餐”和“晚餐”之间的结构完全不同。为什么'晚餐'是NSDictionary而其他人是NSArray?使它们都是相同的类型。如果它们不能,那么必须基于以下条件对您的代码进行条件化:

if ([mealTime isEqualToString: @"Dinner"]) {
   NSDictionary *dinnerDictionary = (NSDictionary *) [MenuDictionary valueForKey:mealTime];
   /* ... */ }

您不需要分配任何内容或从文件中读取任何内容;你已经有了'晚餐'数据的字典。