给定此P-List字典:

我怎么才能到第三站。键-“晚餐”-它本身也是一个字典,并正确地解析它的值?
或者,我是不是应该从一开始就改变P列表的结构,这样我就可以更容易地获得所有东西?
下面是我得到的结果,首先从我的“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);当我找到“晚餐”键时,问题就出现了:它是一个字典,包含2个键和2个数组值。那么如何将其内容加载到Dictionary对象中呢?更具体地说,我应该使用什么'init‘方法将“晚餐”内容加载到我的新Dictionary对象中?
我试过了--不管用:
// I put this inside the first fast-enum loop:
if ([mealTime isEqualToString: @"Dinner"]) {
// init new Dictionary object (declared previously):
dinnerDictionary = [[NSDictionary alloc] initWith ???];我想初始化它与“晚餐”键的内容,但它显然不是一个P-List文件,所以我不能使用
initWithContentsOfFile: pathName我不明白哪个init方法能让我同时访问“晚餐”的键和值。因为尽管“晚餐”被构造为一个字典,但它目前位于一个数组中,该数组并不将其视为字典(我认为……)
显然,我对此有点不清楚。
或者,我是否应该一开始就以不同的方式构造我的P列表,这样我就可以获得这个嵌套的晚餐字典?
有什么想法吗?
发布于 2012-04-22 05:51:11
我认为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];
}
}发布于 2012-04-22 05:34:15
“问题”出在这一行:
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];
/* ... */ }您不需要分配任何内容,也不需要从文件中读取任何内容;您已经有了一个用于“晚餐”数据的字典。
https://stackoverflow.com/questions/10263061
复制相似问题