I want to make a Mac app that downloads official Apple firmware for Mac. To do this I need to get information from their plist.
Is there an API to parse that plist for me so I can more easily query it?
I want to make a Mac app that downloads official Apple firmware for Mac. To do this I need to get information from their plist.
Is there an API to parse that plist for me so I can more easily query it?
You can parse a property list into:
An object graph, using PropertyListSerialization
A model type, using PropertyListDecoder
Be warned, however, that unless Apple has officially documented the location and structure of this property list — and I don’t we have — your overall might run into problems down the line if that location or format changes.
Share and Enjoy
Quinn “The Eskimo!” @ DTS @ Apple
Nice, I see I can use:
PropertyListSerialization.propertyList(from: myDataFromInternet)
but this returns an Any
what do I do with that?
You have a dictionary with 2 keys at the root level, so that Any
you can cast to [String: Any]
like so:
let result = PropertyListSerialization.propertyList(from: myDataFromInternet) as? [String: Any]
andreas66 wrote:
You you can also use Foundation XMLParser to parse it. Or any 3rd
party XML parser you may prefer.
I wouldn’t recommend using an XML parser to parse property lists. To start, it’s a bunch of extra work relative to the property list API. Also, those APIs handle other property list formats, most notably binary property lists.
danmihai wrote:
but this returns an
Any
what do I do with that?
You can, as costinAndronache suggested, conditionally cast it to the type you’re expecting. However, if you’re parsing a property list with a known schema [1], it’s often a lot easier to declare a set of Codable
model types and parse into that using PropertyListDecoder
.
Share and Enjoy
Quinn “The Eskimo!” @ DTS @ Apple
[1] Not that the property list has a known schema, as per the warning in my previous response.