Skip to content

Instantly share code, notes, and snippets.

@eddieh
Created March 4, 2021 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddieh/13bd62141d2b04f3df2815e85b1a0652 to your computer and use it in GitHub Desktop.
Save eddieh/13bd62141d2b04f3df2815e85b1a0652 to your computer and use it in GitHub Desktop.
Write Old-Style ASCII Property Lists like a Boss
#import <Foundation/Foundation.h>
#define BEGIN_AUTORELEASEPOOL @autoreleasepool {
#define END_AUTORELEASEPOOL }
int main(int argc, const char **arg)
{
BEGIN_AUTORELEASEPOOL
NSError *err;
//const char bytes[] = { 0xDE, 0xAD };
NSDictionary *plist = @{
@"one": @1,
@"two": @2,
@"three": @3,
@"stringy": @"noquotesneeded",
@"quoted": @"the spaces require quotes",
@"Null?": NSNull.null,
@"date": NSDate.now,
//@"data": [NSData dataWithBytes:bytes length:2],
};
NSLog(@"plist? %@", plist);
NSLog(@"valid old-style? %@", @([NSPropertyListSerialization
propertyList:plist
isValidForFormat:NSPropertyListOpenStepFormat])
);
NSLog(@"valid plist? %@", @([NSPropertyListSerialization
propertyList:plist
isValidForFormat:NSPropertyListXMLFormat_v1_0])
);
NSLog(@"valid json? %@", @([NSJSONSerialization isValidJSONObject:plist]));
// writes a readable old-style plist except when NSData is included
[plist.description writeToFile:@"/Users/eddie/garbage.plist"
atomically:YES
encoding:NSUTF8StringEncoding
error:&err
];
if (err)
NSLog(@"write err? %@", err);
else
NSLog(@"readable? %@", [NSDictionary
dictionaryWithContentsOfFile:@"/Users/eddie/garbage.plist"]);
END_AUTORELEASEPOOL
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment