2016-12-09 127 views
0

我一直在試圖弄清楚這一天,現在幾天...這是我的原始代碼:無法將'__NSCFNumber'(0x10e59f3c0)類型的值轉換爲'NSString'(0x10dba7ad8)swift 3

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

這裏是我的API:

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

我敢肯定我需要使用 「valueForKey」,所以這裏是我更新的代碼:

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

如何曾經,現在我收到了「類型字符串值:任何沒有成員‘valueForKey’

回答

1

沒關係,我幾乎忘了實際變量更改爲‘雙師型’或數字

var ratingID = 0.0 

,而不是

var ratingID = "" 

和現在的代碼讀取

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

爲什麼不',如果讓ratingDic =辭典[ 「評級」]作爲? [String:Any],讓ratingId = ratingDic [「id」]爲?雙{' –

相關問題