我使用以下代码从推送通知接收自定义数据,但收到以下错误:
无法将类型“__NSArrayM”(0x1b0776cf0)的值转换为“NSDictionary”(0x1b0777128)。
在下面一行:
let jsonData = try? JSONSerialization.jsonObject(with: (customDataString?.data(using: String.Encoding.utf8))!, options: JSONSerialization.ReadingOptions.mutableContainers) as![String: Any]
我该如何解决这个错误?
func onPushAccepted(_ pushManager: PushNotificationManager!, withNotification pushNotification: [AnyHashable : Any]!, onStart: Bool) {
print("Push notification accepted: \(pushNotification!)")
let customDataString = pushManager.getCustomPushData(pushNotification)
if customDataString != nil {
let jsonData = try? JSONSerialization.jsonObject(with: (customDataString?.data(using: String.Encoding.utf8))!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String: Any]
print("*** \(jsonData?["test"]!)")
}
读取错误:
无法转换值expected type Array to offered type 字典
这意味着你的 JSON 是一个数组,所以
if let customDataString = pushManager.getCustomPushData(pushNotification) {
let data = customDataString.data(using: utf8)!
let jsonData = try? JSONSerialization.jsonObject(with: data) as! [[String:Any]]
...
您可以省略options
参数,因为.mutableContainers
无论如何,在 Swift 中是没用的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)