我想将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语句进行检查)。我在这里做错了什么?
答案 0 :(得分:0)
我会分两步完成。首先确保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>
。