• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
[SOLVED2] Code to convert NSDictionary to binary plist file?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[SOLVED2] Code to convert NSDictionary to binary plist file?


  • Subject: [SOLVED2] Code to convert NSDictionary to binary plist file?
  • From: David Hoerl <email@hidden>
  • Date: Wed, 04 Jun 2008 17:08:19 -0400

[For the archives, or anyone else interested in this topic]

Ken Thomases provided a shorter version, by having the NSData instance write the file. Jean-Daniel Dupas suggested using CFPropertyListWriteToStream to bypass the intermediate NSData object (a speed and memory footprint improvement)


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 ];

// 1 - get a XML (text) file representation
    [dict writeToFile:@"/tmp/text.plist" atomically:NO];


// 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;
}
// 2 - have the NSData object save a binary representation
[plist writeToFile:@"/tmp/binary1.plist" atomically:NO];



// 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/binary2.plist" append:NO];
if(str == nil) {
NSLog(@"cannot create output stream");
return -1;
}


// 3 - stream a binary representation
[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; } _______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: Newbie Question on a method signature
  • Next by Date: Re: NSPredicateEditorRowTemplate: Custom templateView state
  • Previous by thread: Re: Newbie Question on a method signature
  • Next by thread: Bindings Cocoa and new Thread (Spinning Beach Ball an new Thread)
  • Index(es):
    • Date
    • Thread