Edit:
The question below incorrectly assumes that because BBEdit displays a property list in XML format, the file actually IS an XML-format plist. It turns out that BBEdit quietly converts binary property lists to XML format and then displays the XML text. Adding to the confusion, if you select the file in the finder and hit the spacebar, the finder's preview also shows an XML version of the contents.
I'm leaving the question, since it is quite confusing. I also self-answered it, and mentioned that you can use the terminal command file <filename> to show info about a property list file, including whether its binary or XML format.
The question also includes code for converting a multi-line text file to a property list that contains an array of strings, one for each line.
Original question, based on a wrong assumption:
I'm trying to create a word list property list file from a text file for a program I'm writing.
Binary property lists are smaller and faster to read than XML property lists, so I tend to prefer them.
To that end, I wrote some code (a macOS command line tool) to read my word list text file, uppercase it, convert it to an array, and write it as a property list.
Changing that code slightly to make it work on a sample inline string, it looks like this:
let filename = "SampleWords"
do {
// For this sample code, simply generate a small set of entries
// separated with newlines. (The real code reads from a file.)
var words =
"""
apple
banana
grape
"""
words = words.uppercased()
let wordsArray = words.components(separatedBy: "\n")
let plistEncoder = PropertyListEncoder()
plistEncoder.outputFormat = .binary // use .xml for xml format
let data = try plistEncoder.encode(wordsArray)
let plistPath = NSString(string: "~/Documents/\(filename).plist").expandingTildeInPath
let plistURL = URL(filePath: plistPath)
try data.write(to: plistURL)
} catch {
print("Error '\(error)' reading or encoding file")
fatalError()
}
And when you run it, the resulting file, SampleWords.plist, looks like this when opened in BBEdit:
<?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">
<array>
<string>APPLE</string>
<string>BANANA</string>
<string>GRAPE</string>
</array>
</plist>
It works, and can be opened as a property list, but it makes the file somewhere around twice as big, and slower to read, than a binary property list.
Am I missing a step somewhere? Why doesn't my setting the outputFormat of my PropertyListEncoder generate a binary property list?
Ok, I feel a little stupid.
It turns out that the above code DOES save my file as a binary property list. However, BBEdit (un)helpfully converts the file to an XML property list and displays THAT.
If I drag the plist file from the above code onto TextEdit, I see the following:
And as HangarRash points out in their comments, the terminal command "file", as in "file ~/Documents/SampleWords.plist", confirms that the file is a binary property list.
It seems that
PropertyListEncodereven defaults to binary plist format. If you don't specify anoutputFormat, you get a binary property list.