gpt4 book ai didi

ios - 如何解析应用程序中的userInfo字典(应用程序: UIApplication, didReceiveRemoteNotification userInfo:[NSObject:AnyObject])

转载 作者:行者123 更新时间:2023-11-30 13:47:56 25 4
gpt4 key购买 nike

我是 Swift 新手,如果这是一个愚蠢的问题,请原谅我。这是我推送到我的应用程序的 JSON:

{
"aps": {
"alert": "Jack Terrance needs your help",
"badge": 1,
"sound": "default",
"data": {
"from": "Jack Terrance",
"from_num": "+1234567890"
}
}
}

我设法接收 JSON,但我不知道如何解析它:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

let message : String = userInfo.map({$0["aps"]!["alert"] as String})

UIAlertView(title: "Notification", message: message!, delegate: nil, cancelButtonTitle: "OK").show();

}

我正在寻找相当于 Objective-C 中的 valueForKeyPath 的东西。

最佳答案

//请尝试以下。希望这对您有用。

if let aps = userInfo["aps"] as? NSDictionary {

if let alert = aps["alert"] as? NSString {
//Do stuff
}else if let badge = alert["badge"] as? NSString {
//Do stuff
}else if let sound = aps["sound"] as? NSString {
//Do stuff
}

if let data = aps["data"] as? NSDictionary {

if let alert = data["from"] as? NSString {
//Do stuff
}else if let from_num = data["from_num"] as? NSString {
//Do stuff
}
}
}

关于ios - 如何解析应用程序中的userInfo字典(应用程序: UIApplication, didReceiveRemoteNotification userInfo:[NSObject:AnyObject]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34693718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com