将字符串转换为NSObject Swift 3
问题描述:
任何人都可以帮助我解决这个问题吗?我得到这个迅速编译器错误:将字符串转换为NSObject Swift 3
'Cannot convert value of type 'String' to expected dictionary key type 'NSObject'
let orderParameters =
["customer": [ <- //Getting the error in this line
"first_name": "Jon",
"last_name":"Doe",
"email": "[email protected]"],
"shipping": "free-shipping",
"gateway": "dummy",
"bill_to": [
"first_name": "Jon",
"last_name": "Doe",
"address_1": "123 Sunny Street",
"address_2": "Sunnycreek",
"city": "Sunnyvale",
"county": "California",
"country": "US",
"postcode": "CA94040",
"phone": "18768509132"],
"ship_to": "bill_to"
] as [NSObject: AnyObject]
难道我插入作为NSObject的为 '客户' 参数?
答
而不是as [NSObject: AnyObject]
,请写as [AnyHashable: Any]
。
这是斯威夫特3的方式来表达一个字典的一般概念。
答
为什么不明NSObject
?钥匙显然都是String
。
在Swift 3中,字典的未指定值类型是Any
。
做一个类型注释
let orderParameters : [String:Any] = ...
,并在年底
删除类型转换(或如Vadian说,完全忽略它。) – matt