Skip to content

Instantly share code, notes, and snippets.

@seiji
Created November 24, 2012 02:57
Show Gist options
  • Save seiji/4138187 to your computer and use it in GitHub Desktop.
Save seiji/4138187 to your computer and use it in GitHub Desktop.
create binary plist
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *err = nil;
NSData *plist;
NSDictionary *dict;
char *payload = "This is the payload";
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Hello world", @"greeting",
[NSData dataWithBytes:payload length:strlen(payload)], @"payload",
[NSNumber numberWithInt:10], @"result",
nil ];
// get a binary file representation - creates an additional NSData in memory object
plist = [NSPropertyListSerialization dataFromPropertyList:dict
format:NSPropertyListBinaryFormat_v1_0
errorDescription:&err];
if(plist == nil) {
NSLog(@"NSPropertyListSerialization error: %@", err);
return -1;
}
// get a binary file representation - no additional memory footprint
// NOTE: documentation does not state it, but NSOutputStream creates the file if it does not yet exist
NSOutputStream *str = [NSOutputStream outputStreamToFileAtPath:@"/tmp/binary.plist" append:NO];
if(str == nil) {
NSLog(@"cannot create output stream");
return -1;
}
[str open];
CFIndex idx = CFPropertyListWriteToStream(dict, (CFWriteStreamRef)str, kCFPropertyListBinaryFormat_v1_0, (CFStringRef *)&err);
if(idx == 0) {
NSLog(@"CFPropertyListWriteToStream error: %x %@", err, err);
return -1;
}
[str close];
[pool drain];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment