Json序列化Swift 3类型错误
我正在使用以下代码从推送通知接收自定义数据,出现以下错误: 无法将类型为“uu NSArrayM”(0x1B077CF0)的值强制转换为“NSDictionary”(0x1B077128) 在以下行:Json序列化Swift 3类型错误,json,swift,swift3,Json,Swift,Swift3,我正在使用以下代码从推送通知接收自定义数据,出现以下错误: 无法将类型为“uu NSArrayM”(0x1B077CF0)的值强制转换为“NSDictionary”(0x1B077128) 在以下行: let jsonData = try? JSONSerialization.jsonObject(with: (customDataString?.data(using: String.Encoding.utf8))!, options: JSONSerialization.ReadingOpti
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"]!)")
}
读取错误:
无法将预期值类型数组转换为提供值类型字典
这意味着您的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中无论如何都是无用的。显然JSON是一个数组,而不是一个字典。你为什么要强制选角?非常感谢