Nij*_*jar 2 dictionary swift swift3
字典的必需扩展名,以获取文本键值(如果存在).
在代码下面实现并成功编译:
extension Dictionary where Key: ExpressibleByStringLiteral, Value: AnyObject {
func getValueForKeyPath(keyValue: String) -> String {
return ((self["item_qty"] as? Dictionary<String, String>) ?? ["": ""])?["text"] ?? ""
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我在方法上做了一些小改动时得到错误:
"对成员'下标'的模糊引用"
extension Dictionary where Key: ExpressibleByStringLiteral, Value: AnyObject {
func getValueForKeyPath(keyValue: String) -> String {
return ((self[keyValue] as? Dictionary<String, String>) ?? ["": ""])?["text"] ?? ""
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在这里做错了,请纠正我.
试着施展keyValue给Key.例如:
extension Dictionary where Key: ExpressibleByStringLiteral, Value: AnyObject {
func getValueForKeyPath(keyValue : String) -> String{
return ((self[keyValue as! Key] as? Dictionary<String,String>) ?? ["":""])?["text"] ?? ""
}
}
Run Code Online (Sandbox Code Playgroud)