【发布时间】:2018-04-12 05:53:52
【问题描述】:
如何快速访问任何 AnyHashable 数据我在下面有我的数据,下面是我单击通知时的日志。当我单击通知时,当我想打印或获取数据日志时,我可以将其加载到视图中。它已经在工作,当我点击通知时它会记录下来,如果我想要的是如何让它加载到视图中。
我做了什么
guard
let aps = userInfo[AnyHashable("aps")] as? NSDictionary,
let alert = aps["alert"] as? NSDictionary,
let body = alert["body"] as? String,
let title = alert["title"] as? String
else {
return
}
print("Title: \(title) \nBody:\(body)")
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
数据
[AnyHashable("aps"): {
alert = {
body = "yuuu - 5.00";
title = "New Chore!";
};
"content-available" = 1;
}, AnyHashable("data"): {
chore = {
desc = huu;
name = yuuu;
pk = 125;
reward = "5.00";
sched = "2018-04-12T09:52:13+08:00";
};
"notification_id" = 16;
pusher = {
publishId = "pubid-01ff965a-20af-4a58-9901-89782043d832";
};
}]
【问题讨论】:
-
您可能正在处理
[AnyHashable:Any]的字典,但在这种情况下,您的键似乎是String,因此您应该能够通过notif["alert"]访问这些元素,例如 -
你可以试试,让strTitle = (userInfo["aps"] as!NSDictionary).value(forKey: "notification") as! NSDictionary let title = strTitle.value(forKeyPath: "alert.title") let body = strTitle.value(forKeyPath: "alert.body")
-
如果我没记错你想提取收到的通知数据对吗?
-
请将其发布为答案先生,以便可以投票。谢谢
-
@AbhirajsinhThakore,没错