2017-02-09 68 views
0

獲取這個數據回爲一個通知,並希望將其讀成可用的變量類型。我得到他們所有的工作,但獎勵和apsSwift 3.0通知可轉換爲數組或字典數組?

%@ [AnyHashable("description"): Open 10 common chests.,  AnyHashable("icon"):  localhost:8000//media/achievements/Common%20Chest%20Go- Getter/bomb.jpg, AnyHashable("name"): Common Chest Go-Getter,  AnyHashable("gcm.message_id"):1486597426811663%bf6da727bf. 6da727, AnyHashable("rewards"): {"Battle helm":1,"Gems":1000}, AnyHashable("aps"): { 
    alert = "Achievement Completed"; 
}] 

那麼如何將其轉換爲可用變量?

AnyHashable("rewards"): {"Battle helm":1,"Gems":1000},  AnyHashable("aps"): { 
    alert = "Achievement Completed"; 
} 

返回的項目被稱爲USERINFO那麼像......

let rewards = userInfo["rewards"] as! [String:Int] 

不工作,有這方面的幫助將不勝感激!我再一次使用swift 3.0,所以swift 3.0的例子可以提供幫助。

回答

1

通知USERINFO被解析爲Dictionary<String, Any>[String: Any]

let userInfo: [String: Any] = [ 
    "description": "Open 10 common chests.", 
    "icon": "localhost:8000//media/achievements/Common%20Chest%20Go- Getter/bomb.jpg", 
    "name": "Common Chest Go-Getter", 
    "gcm.message_id": "1486597426811663%bf6da727bf. 6da727", 

    "rewards": [ 
     "Battle helm": 1, 
     "Gems":1000 
    ], 
    "aps": [ 
     "alert": "Achievement Completed" 
    ] 
] 

if let rewards = userInfo["rewards"] as? [String: Any] { 

    if let battle = rewards["Battle helm"] as? Int { 
    print(battle) // 1 
    } 

    if let gems = rewards["Gems"] as? Int { 
    print(gems) // 1000 
    } 

} 

if let aps = userInfo["aps"] as? [String: Any] { 

    if let alert = aps["alert"] as? String { 
    print(alert) // Achievement Completed 
    } 

} 

看起來,你是混淆的:

let arrayOfStrings = [String]() 
let dictionary = [String: Any]() 

let arrayOfDictionaries = [[String:Any]]() 
相關問題