阅读自定义plist导致崩溃

时间:2013-09-25 15:57:13

标签: ios objective-c nsmutablearray plist

我有一个自定义plist文件,如下所示:

custom plist file

我知道数据在那里(我使用NSLog显示它)。我的问题是我在使用以下代码初始化* hexColors后读取第一行时遇到运行时错误:对象不能为

NSString *pListPath = [[NSBundle mainBundle] pathForResource:@"ColorSchemes" ofType:@"plist"];

// Load the file content and read the data into the array
NSDictionary *colorsDict = [[NSDictionary alloc] initWithContentsOfFile:pListPath];
//  NSLog(@"\ncolorsDict: %@", colorsDict);

NSMutableArray *hexColors = [[NSMutableArray alloc]init];
[hexColors addObject:[colorsDict objectForKey:@"buttons"]];
[hexColors addObject:[colorsDict objectForKey:@"calendar"]];
[hexColors addObject:[colorsDict objectForKey:@"leftGrid"]];
[hexColors addObject:[colorsDict objectForKey:@"topGrid"]];
[hexColors addObject:[colorsDict objectForKey:@"dataGrid"]];

我不明白addObject代码有什么问题(它曾经工作过,没有进行任何更改)。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

如果我没有弄错colorsDict是根词典。它的键是Spring, Autumn, etc.。键button, calendar, leftGrid是您可以通过季节键访问的词典的一部分。

你需要做(例如)

NSDictionary *realColorsDict = [colorsDict objectForKey:@"Spring"];
[hexColors addObject:[realColorsDict objectForKey:@"buttons"]];
[hexColors addObject:[realColorsDict objectForKey:@"calendar"]];
[hexColors addObject:[realColorsDict objectForKey:@"leftGrid"]];
[hexColors addObject:[realColorsDict objectForKey:@"topGrid"]];
[hexColors addObject:[realColorsDict objectForKey:@"dataGrid"]];

如果这可以解决您的问题,请告诉我:))