如何从 Swift 3 中的数据读取属性列表 [英] How can I read a property list from data in Swift 3
问题描述
我正在尝试从 Swift 3 中的 Data
读取 property list
但我无法实现.
I'm trying to read a property list
from Data
in Swift 3 but I can't achieve that.
我尝试过这样的事情:
let datasourceDictionary = try! PropertyListSerialization.propertyList(from: data!, options: [PropertyListSerialization.ReadOptions], format: nil) as! Dictionary
我收到了这个错误:
无法将PropertyListSerialization.ReadOptions.Type"(又名PropertyListSerialization.MutabilityOptions.Type")类型的值转换为预期的元素类型PropertyListSerialization.MutabilityOptions"
Cannot convert value of type 'PropertyListSerialization.ReadOptions.Type' (aka 'PropertyListSerialization.MutabilityOptions.Type') to expected element type 'PropertyListSerialization.MutabilityOptions'
然后我尝试了类似我以前在 Swift 1.2 上做的事情:
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
我收到了这个错误:
'immutable' 不可用:使用 [] 构造一个空的选项集
'immutable' is unavailable: use [] to construct an empty option set
然后我尝试了这个:
let datasourceDictionary = try! PropertyListSerialization.propertyList(from: data!, options: [], format: nil) as! Dictionary
我收到了这个错误:
'[Any]' 不能转换为 'PropertyListSerialization.ReadOptions'(又名 'PropertyListSerialization.MutabilityOptions')
'[Any]' is not convertible to 'PropertyListSerialization.ReadOptions' (aka 'PropertyListSerialization.MutabilityOptions')
如何从 Swift 3 中的数据"中读取 property list
文件,或者有什么方法可以做到这一点?
How can I read the property list
file from `Data in Swift 3 or what is the way to do that?
推荐答案
Dictionary
是一个通用类型,它需要键和值的类型信息.
Dictionary
is a generic type which needs type information for the keys and the values.
使用Dictionary
或更短的[String:Any]
:
let datasourceDictionary = try! PropertyListSerialization.propertyList(from:data!, format: nil) as! [String:Any]
options
参数可以省略.
这篇关于如何从 Swift 3 中的数据读取属性列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!