Json Serialisation Swift 3 type error

前端 未结 1 1487
离开以前
离开以前 2021-01-29 14:21

I am using the following code to receive custom data from a push notification and I get the following error:

Could not cast value of type \'__NSArrayM\' (

1条回答
  •  無奈伤痛
    2021-01-29 14:52

    Read the error:

    Could not cast value of expected type Array to offered type Dictionary

    That means your JSON is an array, so

    if let customDataString = pushManager.getCustomPushData(pushNotification) {   
       let data = customDataString.data(using: utf8)!
       let jsonData = try? JSONSerialization.jsonObject(with: data) as! [[String:Any]]
       ...
    

    You can omit the options parameter, because .mutableContainers is useless in Swift anyway.

    0 讨论(0)
提交回复
热议问题