将数据从plist转换为对象时遇到了一些问题。
plist具有以下结构
我使用以下代码
读取文件-(void)readAnimationsFromPlist
{
NSDictionary *dict;
NSString *path = [[NSBundle mainBundle] pathForResource:@"Animationen" ofType:@"plist"];
dict = [[NSDictionary alloc] initWithContentsOfFile:path];
CCLOG(@"%@", [dict description]);
for (NSDictionary *items in dict)
{
Animation *animation = [[Animation alloc] init];
animation.name = items.description;
CCLOG(@"%@", items);
animation.delay = [items valueForKey:@"delay"]; //(1)
animation.phases = [items valueForKey:@"phases"];
CCLOG(@"Animation %@ mit %i frames eingelesen", items.description, animation.phases.count);
[animationen setObject:animation forKey:animation.name];
[animation release]; //(2)
}
[dict release];
CCLOG(@"%i animationen eingelesen", [animationen count]);
}
现在我的问题是,没有数据在标有(1)的行中读取会抛出以下异常。
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSCFString 0x55d3ba0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key delay.'
我找到了有关此消息的一些信息,但没有任何帮助。
当我在调试时查看检查器窗口时,它显示项目的类型为NSCFString,值为'Hauptgewinn',但它应该是字典。我试图将它明确地转换为NSDictionary,但没有任何改变。
我该怎么做才能解决这个问题?
在位置(2)我必须释放那里或者我可以删除这一行吗?
答案 0 :(得分:0)
// 2是正确的。
-(void)readAnimationsFromPlist
{
NSDictionary *dict;
NSString *path = [[NSBundle mainBundle] pathForResource:@"untitled" ofType:@"plist"];
dict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSLog(@"Dictionary is: %@", dict);
for(id key in dict)
{
Animation *animation = [[Animation alloc] init];
animation.name = key;
CCLOG(@"%@", key);
animation.delay = [[dict valueForKey:key] valueForKey:@"delay"]; //(1)
animation.phases = [[dict valueForKey:key] valueForKey:@"phases"];
CCLOG(@"Animation %@ mit %i frames eingelesen", items.description, animation.phases.count);
[animationen setObject:animation forKey:animation.name];
[animation release]; //(2)
}
[dict release];
}