【发布时间】:2017-11-03 18:54:50
【问题描述】:
通过下标语法访问 Swift Dictionary 值会导致错误 Ambiguous reference to member 'subscript'
这里是代码
class Model {
struct Keys {
static let type :String = "type" //RowType
static let details :String = "details"
}
var type :RowType = .None
var details :[Detail] = []
init(with dictionary:Dictionary<String, Any>) {
if let type = dictionary[Keys.type] as? String {
self.type = self.rowTypeFromString(type: type)
}
if let detailsObj = dictionary[Keys.details] as? Array { //Error : Ambiguous reference to member 'subscript'
}
}
}
如果我在可选绑定结束时删除类型转换as? Array,它编译得很好
我期望details 键的值是Array,我知道我可以使用[String,Any] 而不是Dictionary<Key, Value>,是什么导致了这个问题?
【问题讨论】:
-
as? Array– 作为 what 的数组?
标签: swift dictionary collections