【问题标题】:plist read failuresplist 读取失败
【发布时间】: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


    【解决方案1】:

    //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];
    }
    

    【讨论】:

    • 但这仅适用于单个动画。在我的 plist 中应该有更多的动画(就像类名所暗示的那样:)。我创建了更多与第一个条目平行的动画。我应该在上面添加另一本字典来阅读吗?
    • 谢谢,现在可以了。没有看到还有另一个层次的字典来看看。我发现调试时看不到对象中的数据很烦人。我只看到该死的指针,没有数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 2023-01-08
    • 2012-02-04
    相关资源
    最近更新 更多