Why can't I serialize this data?
I want to serialize an array and an integer. When I save only the integer, it works. When I add the array, it fails.
NSArray *tempArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [tempArray objectAtIndex:0];
tempArray = nil;
tempArray = [NSArray arrayWithObjects:
( (someOtherArray != nil) ? (id) someOtherArray : [NSNull null] ),
[NSNumber numberWithInt:someIntValue],
nil];
NSString *filePath = [NSString stringWithFormat:@"%@/%@.plist", docsPath, kDataFilename];
NSString *errString;
NSData *serializedStrings= [NSPropertyListSerialization
dataFromPropertyList:tempArray
format:NSPropertyListBinaryFormat_v1_0
errorDescription:&errString];
if( errString != nil ) {
NSLog(@"SaveData NSpropertyListSerialization error: %@", [errString description]);
[errString release];
}
// returns NO
BOOL success = [serializedStrings writeToFile:filePath atomically:YES];
The array may be nil so I do a check for that first. I also tried [NSNumber numberWithInt:0] instead of [NSNull null] just to see if it would work instead of null, but it didn't.
I also开发者_运维知识库 get: NSpropertyListSerialization error: Property list invalid for format
You can't store +[NSNull null]
in a plist. You need a workaround like a placeholder value (even though NSNull
is itself a placeholder value...)
精彩评论