将String转换为NSObject Swift 3

时间:2016-11-14 21:05:18

标签: swift string swift3 nsdictionary nsobject

任何人都可以帮我解决这个问题吗?我收到了这个快速的编译器错误:

  

'无法将'String'类型的值转换为预期的字典键类型'NSObject'

let orderParameters =

        ["customer": [     <- //Getting the error in this line
            "first_name": "Jon",
            "last_name":"Doe",
            "email": "john.doe@gmail.com"],
       "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]

我是否为'customer'参数插入 作为NSObject

2 个答案:

答案 0 :(得分:0)

而不是as [NSObject: AnyObject],请写as [AnyHashable: Any]

这就是Swift 3表达字典概念的方法。

答案 1 :(得分:0)

为什么未指定NSObject?密钥显然都是String

在Swift 3中,字典的未指定值类型为Any

制作类型注释

let orderParameters : [String:Any] = ...

并删除结尾处的类型