pableiros pableiros - 2 months ago 75
Swift Question

How can I read a property list from data in Swift 3

I'm trying to read a

property list
from
Data
in Swift 3 but I can't achieve that.

I'm tried something like this:

let datasourceDictionary = try! PropertyListSerialization.propertyList(from: data!, options: [PropertyListSerialization.ReadOptions], format: nil) as! Dictionary


and a I got this error:


Cannot convert value of type 'PropertyListSerialization.ReadOptions.Type' (aka 'PropertyListSerialization.MutabilityOptions.Type') to expected element type 'PropertyListSerialization.MutabilityOptions'


Then I tried something like this like I used to do on Swift 1.2:

let datasourceDictionary = try! PropertyListSerialization.propertyList(from: data!, options: [PropertyListSerialization.MutabilityOptions.immutable], format: nil) as! Dictionary


And I got this error:


'immutable' is unavailable: use [] to construct an empty option set


Then I tried this:

let datasourceDictionary = try! PropertyListSerialization.propertyList(from: data!, options: [], format: nil) as! Dictionary


and I got this error:


'[Any]' is not convertible to 'PropertyListSerialization.ReadOptions' (aka 'PropertyListSerialization.MutabilityOptions')


How can I read the
property list
file from `Data in Swift 3 or what is the way to do that?

Answer

Dictionary is a generic type which needs type information for the keys and the values.

Use Dictionary<String,Any> or shorter [String:Any]:

let datasourceDictionary = try! PropertyListSerialization.propertyList(from:data!, options: [], format: nil) as! [String:Any]
Comments