我试图这样做,但它说
Value of type ‘AnyObject?’ has no member ‘Generator’
所以这是我的代码.
let dataDictionary:NSDictionary = try NSJSONSerialization.JSONObjectWithData(responSEObject as! NSData,options: NSJSONReadingOptions.MutableContainers) as! NSDictionary var customerArray = dataDictionary.valueForKey("kart") for js: NSDictionary in customerArray { let nameArray: NSArray = js.valueForKey("name") }
我做错了什么.我没弄明白.谢谢你的帮助.
解决方法
您的customerArray是一个Optional,它的类型是AnyObject吗? (这是因为.valueForKey返回一个Optional).你不能循环使用Optional.解决方案是在安全展开时将结果转换为数组:
if let customerArray = dataDictionary.valueForKey("kart") as? NSArray { for js in customerArray { let nameArray = js.valueForKey("name") // ... } }