我有一个PHP脚本,它以数组文字的格式返回JSON响应。
确切返回的示例:
["school","chess_club"]
但是,在我成功收到回复后,我无法在Swift中解析它。
代码:
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String:AnyObject]
print (json)
错误:
无法转换类型' __ NSArrayI' (0x10a440d88)到' NSDictionary' (0x10a441288)。
请注意重复:
我发现这个副本Could not cast value of type '__NSArrayI' (0x10df73c08) to 'NSDictionary' (0x10df74108),但我无法弄清楚如何在我的情况下使用它来修复错误。
具体来说,我不知道如何将它转换为NSDictionary,因为我没有在我的代码中指定任何地方" NSDictionary"。
答案 0 :(得分:1)
[String:AnyObject]
是Dictionary
对象,但您没有指定它。试试这个:
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String]
print (json)