且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法将类型'__NSDictionaryM'的值强制转换为'NSArray';

更新时间:2023-02-04 09:31:55

错误消息说

无法将 actual 类型的NSDictionary强制转换为 expected 类型的NSArray

Could not cast the actual type NSDictionary to the expected type NSArray

[NSDictionary]表示字典数组,但是response的值显然是字典,由一对大括号表示.

[NSDictionary] means an array of dictionaries, but the value of response is clearly a dictionary, represented by a pair of braces.

实际上是

let quoteDictionary = quotesData["response"] as! NSDictionary

但是建议使用Swift本机集合类型

But it's recommended to use Swift native collection types

let quoteDictionary = quotesData["response"] as! Dictionary<String,AnyObject>