将NSDictionary保存为plist:序列化错误

时间:2011-08-12 15:54:58

标签: objective-c plist nsdictionary

我的应用程序中有一个包含嵌套字典的字典(根据“闪存卡”)。出于某种原因,NSDictionary不会写入文件。如果我只是在我的字典上调用writeToFile,它就不起作用,并且NSPropertyListSerialization返回属性列表对Format无效。代码如下:

#import "flashCard.h"

#define kFront @"front"
#define kBack @"back"

@implementation flashCard

- (id) init
{
if (self = [super init])
{
    cards = [[NSDictionary alloc] initWithObjectsAndKeys:
             [[NSDictionary alloc] initWithObjectsAndKeys:
              @"aetas/aetatis", kFront,
              @"lifetime, age, generation", kBack,
              nil], [NSNumber numberWithInt: 1],
             [[NSDictionary alloc] initWithObjectsAndKeys:
              @"amicitia/amicitiae", kFront,
              @"alliance, friendship", kBack,
              nil], [NSNumber numberWithInt: 2],
             [[NSDictionary alloc] initWithObjectsAndKeys:
              @"amicus/amici", kFront,
              @"friend", kBack,
              nil], [NSNumber numberWithInt: 3],
             [[NSDictionary alloc] initWithObjectsAndKeys:
              @"animus/animi", kFront,
              @"mind, heart, spirit, courage", kBack,
              nil], [NSNumber numberWithInt: 4],
             nil];

    currentSide = [[NSString alloc] init];

    currentCard = 1;
    currentSide = kFront;
}

return self;
}

稍后,在相同的实现中......

- (void) saveSet
{
NSLog(@"saveSet called");
NSSavePanel *cocaoSavePanel = [NSSavePanel savePanel];

int buttonPressed = [cocaoSavePanel runModal];
if (buttonPressed != NSOKButton)
{
    return;
}

NSLog(@"ok button, with url: %@", [cocaoSavePanel URL]);

NSError *error;
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:cards
                     format:NSPropertyListXMLFormat_v1_0
                                                    options: 0
                     error: &error];

if (plistData)
{
    [plistData writeToURL:[cocaoSavePanel URL] atomically:YES];
    NSLog(@"File Saved Successfully");
}
else {
    NSLog(@"%@", error);
}
}

任何帮助都将不胜感激。

编辑:修改后:

2011-08-12 13:00:47.937 flashcards[14452:a0f] saveSet called
2011-08-12 13:00:48.763 flashcards[14452:a0f] ok button, with url: file://localhost/Users/sam/Desktop/asdf
2011-08-12 13:00:48.764 flashcards[14452:a0f] {
    3 =     {
        back = friend;
        front = "amicus/amici";
    };
    2 =     {
        back = "alliance, friendship";
        front = "amicitia/amicitiae";
    };
    1 =     {
        back = "lifetime, age, generation";
        front = "aetas/aetatis";
    };
    4 =     {
        back = "mind, heart, spirit, courage";
        front = "animus/animi";
    };
}
2011-08-12 13:00:48.764 flashcards[14452:a0f] Property list invalid for format: 100
Program received signal:  “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all

1 个答案:

答案 0 :(得分:2)

currentSide = [[NSString alloc] init];

currentCard = 1;
currentSide = kFront;

泄漏空NSString(或者,会{,} [[NSString alloc] init]同时返回一个单身人士)。此外,您正在泄漏放入外部cards词典的所有词典。

通常,当弹​​出这种错误时,这是​​因为你的词典/数组中有一个非plist兼容的对象;不是NSNumber,NSArray,NSDate,NSData,NSString,NSDictionary等的东西....

在序列化之前

NSLog(@"%@", cards);并发布。还有什么是精确的plist序列化格式错误?


我只是复制/粘贴你的代码并得到:

asdfasdf[81731:707] Property list invalid for format (property list dictionaries may only have keys which are CFStrings, not 'CFNumber')
  • 您不能拥有数字键

  • 始终阅读并发布在运行时发出的全部错误消息