【发布时间】:2011-09-05 23:39:26
【问题描述】:
我在将数据从 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.'
我找到了一些关于此消息的信息,但没有任何帮助。
- 不存在绑定问题(未使用 IB)
- 当我使用 objectForKey 时出现另一个错误
当我在调试时查看检查器窗口时,它显示项目的类型为 NSCFString,值为“Hauptgewinn”,但它应该是字典。我尝试将其显式转换为 NSDictionary,但没有任何变化。
我能做些什么来解决这个问题?
在位置 (2) 上,我必须在那里释放它还是可以删除这一行?
【问题讨论】:
标签: objective-c plist nsdictionary