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

fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
  • @JasonCoco Not true. See answer by Ole Begemann. – jcsahnwaldt Reinstate Monica Aug 16 '13 at 13:25
  • 1
    @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. – Jason Coco Aug 16 '13 at 20:15
  • @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. – jcsahnwaldt Reinstate Monica Aug 19 '13 at 21:47
  • 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). – Thomas Tempelmann Mar 19 '20 at 13:06
  • The actual format of the archived keys & values is document here to some extent: https://www.mac4n6.com/blog/2016/1/1/manual-analysis-of-nskeyedarchiver-formatted-plist-files-a-review-of-the-new-os-x-1011-recent-items – Thomas Tempelmann Mar 19 '20 at 13:34

1 Answers1

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.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • 1
    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. – Dan Rosenstark Mar 17 '16 at 21:42
  • 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. – Thomas Tempelmann Mar 19 '20 at 13:07