I'm trying to save data to a file in binary format so the file size is as small as possible. But it seems that NSPropertyListFormat.BinaryFormat_v1_0 does not actually create binary data but instead creates an XML-style format exactly the same as NSPropertyListFormat.XMLFormat_v1_0. Am I doing something wrong? Here is some test code:
@IBAction func archiveAction(sender: AnyObject) {
let firstName = "Robert"
let lastName = "Smith"
var md: NSMutableData = NSMutableData()
var archiver = NSKeyedArchiver(forWritingWithMutableData:md)
archiver.outputFormat = NSPropertyListFormat.BinaryFormat_v1_0
archiver.encodeObject(firstName, forKey: "first")
archiver.encodeDouble(123.456, forKey: "double")
archiver.encodeFloat(Float(M_PI), forKey: "pi")
archiver.encodeObject(lastName, forKey: "last")
archiver.finishEncoding()
let filePath = NSTemporaryDirectory().stringByAppendingPathComponent("FileNameBinary")
md.writeToFile(filePath, atomically: false)
println("done")
}
When I examine the file this creates I see that it is an XML file. And it has exactly the same number of bytes as it has if I change the .BinaryFormat_v1_0 to .XMLFormat_v1_0.
So how can save data as binary?
EDIT: Here is the file contents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>$archiver</key>
<string>NSKeyedArchiver</string>
<key>$objects</key>
<array>
<string>$null</string>
<string>Robert</string>
<string>Smith</string>
</array>
<key>$top</key>
<dict>
<key>double</key>
<real>123.456</real>
<key>first</key>
<dict>
<key>CF$UID</key>
<integer>1</integer>
</dict>
<key>last</key>
<dict>
<key>CF$UID</key>
<integer>2</integer>
</dict>
<key>pi</key>
<real>3.1415927410125732</real>
</dict>
<key>$version</key>
<integer>100000</integer>
</dict>
</plist>