gpt4 book ai didi

ios - 转换到推送通知字典和从推送通知字典转换的正确方法

转载 作者:行者123 更新时间:2023-11-28 08:45:46 26 4
gpt4 key购买 nike

我想将 userInfo["aps"] 转换为 application: UIApplication, didReceiveRemoteNotification 中的字典。我目前的代码是:

HTTPManager.sharedInstance.pushPayload = userInfo["aps"] as! Dictionary<String, AnyObject>.

pushPayload 定义为:var pushPayload: AnyObject?

现在我尝试使用以下方法解包此有效负载:

if let message = HTTPManager.sharedInstance.pushPayload?["alert"] as? String {
print("WE HAVE A PAYLOAD AND THE MESSAGE IS: \(message)")

} else {
print("Nothing in the payload")
}

当我尝试打开它时,我总是得到 nil,即使有效负载确实包含对象(我正在使用 print 语句进行检查)。我在这里做错了什么?

最佳答案

我会分两步完成。确保 pushPayload首先是正确的类型,然后检索值。试试这个:

if let dict = HTTPManager.sharedInstance.pushPayload as? [String:AnyObject], let message = dict["alert"] {
print("WE HAVE A PAYLOAD AND THE MESSAGE IS: \(message)")
} else {
print("Nothing in the payload")
}

此外,我认为不需要 as! Dictionary<String, AnyObject>在这种情况下。

关于ios - 转换到推送通知字典和从推送通知字典转换的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35406127/

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