Swift 3.0 通知可转换为数组或字典数组?
Swift 3.0 notification transformable to array or array of dictionaries?
正在获取此数据作为通知并希望将其读入可用的变量类型。我让他们所有人都工作,但奖励和 aps
%@ [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 示例可能有所帮助。
通知 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]]()
正在获取此数据作为通知并希望将其读入可用的变量类型。我让他们所有人都工作,但奖励和 aps
%@ [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 示例可能有所帮助。
通知 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]]()