I was trying to save a Set<String>
type in User Defaults and got a
"Attempt to set a non-property-list object" error..
which got me wondering, why is Set not a property list type?
Why are arrays and dictionaries allowed, but not sets?
Edit: I am aware of other ways to store my data. I'm just wondering if there is a known or estimated reason why Apple decided that Sets are not property list types.
Is it because Sets do not have keys like Arrays (index) and Dictionaries (key)? Is it because Sets are unordered? But Dictionaries are unordered as well..
Property lists represent data, not implementation details. An array is an ordered list. A dict is a mapping of string keys to values. What is a "set?" It doesn't mean anything special in simple text data. If the list cannot have duplicates, don't write duplicates. If it's unordered, ignore the order. Everything "set-like" has to do with manipulation of the data. (What behavior would you expect from a plist-set that actually did have duplicate elements? Would it be invalid? If not, how is it not just an array?)
Property lists are not a general object-tree serialization tool. That's what NSCoder is for. They're primarily designed for human-writable configuration like Info.plist, and to be parseable in any language. NSUserDefaults also is not a general purpose data-store (if it were, it'd be called "NSApplicationDataStore" or something like that). It was designed to store user configuration data in a way that users could hand-edit it.
The original (NeXTSTEP) format was much simpler to type:
SomeKey = {
MyFirstProperty = ("here's", "a", "list");
SomeOtherThing = "I'm a string";
};
Apple switched to XML with OS X, making it much harder to type, but the basic approach was the same. (During the early days of OS X, Apple made a lot of moves to integrate Java into the system, including more reliance on XML including formal DTDs. In retrospect, many of those moves were probably unfortunate, but they made sense at the time.)
Plists solve the same problems as JSON. They provide a minimal set of types that are are required to encode raw data, not data structures for implementing algorithms.
I'm not saying that plists were created from some mathematical first principles. If NSSet were an incredibly common type in configuration data, I'm certain that it would have found its way into the format. But they just weren't invented to be a generic way to save arbitrary application data, and neither was NSUserDefaults.