Len_X
Len_X

Reputation: 863

Cannot convert value of type 'NSDictionary' to specified type 'Dictionary'

Why is this possible:

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

But this is not:

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


    ]

Getting error :Cannot convert value of type 'NSDictionary' to specified type 'Dictionary'

I need it to work like this because I have to append data to it like so:

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

Upvotes: 2

Views: 4977

Answers (3)

Yuchen
Yuchen

Reputation: 33036

This should be what you want

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

And this works too with string interpolation:

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

And also notice that you can use let instead of var if the dictionary is immutable.

Upvotes: 3

Sanju
Sanju

Reputation: 1168

Can't we write NSDictionary instead of Dictionary. It works too.

We need to stop using NSDictionary in swift? where Dictionary is native for 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)

Upvotes: 0

Annie Gupta
Annie Gupta

Reputation: 2822

For swift2 or swift3 you need to specify the type of key and value for dictionary. So use the below code-

        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]

Upvotes: 0

Related Questions