I have a dictionary with nested dictionaries (as per “flash cards”) in my application. For some reason, the NSDictionary will not write to file. If I simply call writeToFile on my dictionary, it doesn’t work, and NSPropertyListSerialization returns Property List invalid for Format. The code is as follows:
#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;
}
later, in the same implementation…
- (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);
}
}
Any help would be appreciated.
EDIT: after revisions:
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
That leaks the empty
NSString(or, would, but that[[NSString alloc] init]returns a singleton coincidentally). Also, you are leaking all of the dictionaries that you put into the outercardsdictionary.Generally, when this kind of error pops up, it is because you have a non-plist-compatible object in your dictionaries/arrays; something that isn’t an NSNumber, NSArray, NSDate, NSData, NSString, NSDictionary, etc….
NSLog(@"%@", cards);right before serialization and post that. Also what is the exact plist serialization format error?I just copy/pasted your code and got:
you can’t have keys that are numbers
always read and post the entirety of the error messages spewed at runtime