【发布时间】:2017-03-29 19:24:43
【问题描述】:
我正在使用以下代码从推送通知接收自定义数据,我收到以下错误:
无法将“__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"]!)")
}
【问题讨论】:
-
显然 JSON 是一个数组,而不是字典。你为什么要强制转换?