Denis
Denis

Reputation: 578

Could not cast value of type '__NSCFNumber' (0x10e59f3c0) to 'NSString' (0x10dba7ad8) swift 3

I've been tying to figure out this one for a few days now... Here is my original code:

if let ratingDic = dictionary["rating"] as? [String: Any],
        let ratingId = ratingDic["id"] {
        searchResult.ratingID = ratingId as! String
    }

Here is my api:

"amount" = "50.00";
"rating" =     {
    "name" = "Platinum";
    "id" = 5

I'm pretty sure I need to use "valueForKey" so here is my updated code:

if let ratingDic = dictionary["rating"] as? [String: Any],
        //let ratingId = ratingDic["id"] {
        let ratingId = [ratingDic.valueForKey("id")!] {
        searchResult.ratingID = ratingId as! Number
    }

However now I receive the "Value for type String:Any has no member "valueForKey"

Upvotes: 0

Views: 2655

Answers (1)

Denis
Denis

Reputation: 578

Never mind, I was forgetting to change the actual variable to a "Double" or number

var ratingID = 0.0

instead of

var ratingID = ""

and the code now reads

if let ratingDic = dictionary["rating"] as? [String: Any],
        let ratingId = ratingDic["id"] {
        searchResult.ratingID = ratingId as! Double
    }

Upvotes: 1

Related Questions