What format does NSKeyedArchiver save?
Asked Answered
A

1

9

When I use NSKeyedArchiver is the data that is written a *.plist, I have seen some examples where people have the output file down as *.txt or even without an extension at all?

-(void)saveCore {
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:reactorCore forKey:@"CORE"];
    [archiver finishEncoding];
    [data writeToFile:[self dataFilePath] atomically:YES];

    [data release];
    [archiver release];
}

gary

Abiogenetic answered 15/3, 2010 at 14:39 Comment(5)
@JasonCoco Not true. See answer by Ole Begemann.Laxity
@JonaChristopherSahnwaldt It is true. The data that's generated is binary plist data, and the way it's structured is completely proprietary. Yes, you can convert this binary plist data to something else, like an xml plist, but it doesn't make how the plist is organized any less proprietary, nor does it mean you can assume any given form. It could easily change between versions if they wanted it to and has in the past.Landing
@JasonCoco I don't understand what you mean by 'binary plist'. I thought you meant a binary file format, and XML is not a binary format. And as far as I know, the plist XML format hasn't been changed since its conception.Laxity
Well, @JasonCoco is right that the data written by NSKeyedArchiver is difficult to interpret. That's because while it may be written in a readable plist or xml format, the contents are still cryptic, i.e. not in a straight-forward key-value arrangement. Instead it's in internal & undocumented format. It's not likely to change, though (the change that happened was going from an unkeyed to a keyed format, but that also required the use of new APIs).Ubald
The actual format of the archived keys & values is document here to some extent: mac4n6.com/blog/2016/1/1/…Ubald
C
15

You can use any file extension you want. It is completely unrelated to the actual file format NSKeyedArchiver uses. By default, the archive will be in binary form, but if you set the archiver's outputFormat property to NSPropertyListXMLFormat_v1_0, it will write an XML plist. And when you do that, you should probably give your file a .plist or .xml extension.

Curler answered 15/3, 2010 at 17:46 Comment(2)
This is 100% correct. Note that if you open a binary plist file in TextMate, it will automatically convert to ASCII (with the "!!! BINARY PROPERTY LIST WARNING !!!" at the top).... which might be confusing for one.Buckeye
BBEdit can make binary plists (.plist) readable as XML plists. That way, you can save the data into a .plist file and open it in BBEdit to read it. Still, the contents will be difficult to understand.Ubald

© 2022 - 2024 — McMap. All rights reserved.