无法转换类型的价值' NSDictionary'指定类型'字典'

时间:2016-11-04 00:15:38

标签: ios dictionary swift2 nsdictionary

为什么会这样:

var orderRequestUserValues : Dictionary  = [ "ClientID": "TEST",
                                                 "UserName": "sysadmin",
                                                 "Password": "123456",
                                                 "ModuleID": "StockProcessing",
                                                 "FunctionID": "SetStockActivity",
                                                 "TransmissionFlags": 32,
                                                 "TransmissionMethod": 5
    ]

但这不是:

let userName : String = (dataSourceForUserInfo.last?.UserName)!
let password : String = (dataSourceForUserInfo.last?.Password)!
let clientID : String = (dataSourceForUserInfo.last?.ClientID)!
var orderRequestUserValues : Dictionary  = [  "ClientID": "\(clientID)",
                                              "UserName": "\(userName)",
                                              "Password": "\(password)",
                                              "ModuleID": "StockProcessing",
                                              "FunctionID": "SetStockOrder",
                                              "TransmissionFlags": 32,
                                              "TransmissionMethod": 5


    ]

获取错误:无法转换类型的值' NSDictionary'指定类型'字典'

我需要它像这样工作,因为我必须像这样附加数据:

orderRequestUserValues.updateValue(requestParameters, forKey: "RequestParameters")

3 个答案:

答案 0 :(得分:3)

这应该是你想要的

var orderRequestUserValues : [String : Any]  = [
    "ClientID": "TEST",
    "UserName": "sysadmin",
    "Password": "123456",
    "ModuleID": "StockProcessing",
    "FunctionID": "SetStockActivity",
    "TransmissionFlags": 32,
    "TransmissionMethod": 5
]

这也适用于字符串插值:

var orderRequestUserValues : [String : Any]  = [
    "ClientID": "\(clientID)",
    "UserName": "\(userName)",
    "Password": "\(password)",
    "ModuleID": "StockProcessing",
    "FunctionID": "SetStockOrder",
    "TransmissionFlags": 32,
    "TransmissionMethod": 5
]

并且还注意到,如果字典是不可变的,则可以使用let而不是var

答案 1 :(得分:0)

对于swift2或swift3,您需要指定字典的键类型和值。所以使用下面的代码 -

        let userName : String = (dataSourceForUserInfo.last?.UserName)!
        let password : String = (dataSourceForUserInfo.last?.Password)!
        let clientID : String = (dataSourceForUserInfo.last?.ClientID)!
        let orderRequestUserValues : Dictionary  = [  "ClientID": "\(clientID)",
                                                      "UserName": "\(userName)",
                                                      "Password": "\(password)",
                                                      "ModuleID": "StockProcessing",
                                                      "FunctionID": "SetStockOrder",
                                                      "TransmissionFlags": 32,
                                                      "TransmissionMethod": 5


            ] as [String : Any]

答案 2 :(得分:0)

我们不能写NSDictionary而不是Dictionary。它也有效。

我们需要在swift中停止使用NSDictionary吗?其中Dictionary是Swift struct的原生

 let orderRequestUserValues : NSDictionary  = ["ClientID": "\(clientID)",
                                                  "UserName": "\(userName)",
                                                  "Password": "\(password)",
                                                  "ModuleID": "StockProcessing",
                                                  "FunctionID": "SetStockOrder",
                                                  "TransmissionFlags": 32,
                                                  "TransmissionMethod": 5]


    let name = orderRequestUserValues.object(forKey: "Username")
    print(name)