简体   繁体   中英

Not able to access values from dictionary

I have a dictionary like so..

{
    "Due_Date" = "2020-06-09T15:00:13";
    departmentID = 180075;
    keyPointID = "";
    id = 4;
    jobID = 180093;
    jobName = myLab;
    plantID = 1232;
    shiftID = 2;
    "smat_Leader1ID" = 43232;
    workshopID = 423423;
    workstationID = 1892074;
}

I'm not able to get any values except those values whose key and value is string (Like Due_Date). I have fetched it like so...

var Due_Date =  self.dictionary["Due_Date"]! as? String //This gives an answer

Similarely, if I try to fetch these values, I don't get the values

var Department_ID = self.dictionary["departmentID"]! as? Int ?? 0   //This gives 0
var smatLeader1ID = self.dictionary["smat_Leader1ID"]! as? Int ?? 0 //This gives 0 

(I don't get the other values also. Just that I mentioned just 2 above)

Since you're force casting and not getting a crash you seem to be getting the value but you're trying to cast the value as Int which is failing here. Seems the value is not an Int type could be String try out this to fix it.

Replace this:

var Department_ID = self.dictionary["departmentID"]! as? Int ?? 0 

With this:

var Department_ID = Int(self.dictionary["departmentID"] as? String ?? "0")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM