Swift JSON error : Could not cast value of type '__NSDictionaryM' to 'NSArray'

后端 未结 3 453
自闭症患者
自闭症患者 2020-12-03 18:45

when decoding JSON from webservice(API) i get error :

Could not cast value of type \'__NSDictionaryM\' (0x1037ad8a8) to \'NSArray\' (0x1037ad470). 
         


        
3条回答
  •  不知归路
    2020-12-03 19:30

    The bitnami response starts with a { and it is therefore a JSON object, which corresponds to an NSDictionary. The other one starts with [ which indicates an array.

    You need to change the type of json to Dictionary, and deserialize as follows:

    json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! Dictionary
    

提交回复
热议问题