如何在Swift 3中从数据读取属性列表 [英] How can I read a property list from data in Swift 3

查看:182
本文介绍了如何在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'(aka'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>或更短的[String:Any]:

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

options参数可以省略.

这篇关于如何在Swift 3中从数据读取属性列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆