正在将NSDictionary保存到plist:序列化错误



我的应用程序中有一个带有嵌套字典的字典(按照"闪存卡"(。由于某些原因,NSDictionary不会写入文件。如果我只是在字典上调用writeToFile,它就不起作用,并且NSPropertyListSerialization返回的Property List对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
currentSide = [[NSString alloc] init];
currentCard = 1;
currentSide = kFront;

这会泄漏空的NSString(或者,会,但[[NSString alloc] init]会同时返回一个singleton(。此外,您正在泄漏放入外部cards字典中的所有字典。

通常,当出现这种错误时,是因为您的字典/数组中有一个不兼容的对象;不是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')
  • 你不能有数字的钥匙

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

最新更新