Lenx van Zyl Lenx van Zyl - 1 month ago 8
iOS Question

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")

Answer

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:

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