I am trying to do this but it's says
我试图这样做,但它说
Value of type 'AnyObject?' has no member 'Generator'
类型'AnyObject?'的值没有成员'发电机'
So this is my code.
所以这是我的代码。
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")
}
What I am doing wrong.I didn't figure out.Thank you for your helps.
我做错了什么。我没弄明白。谢谢你的帮助。
2 个解决方案
#1
8
Your customerArray
is an Optional, it has the type AnyObject?
(this is because .valueForKey
returns an Optional). You can't loop over an Optional. Solution is to cast the result as an array while safe unwrapping:
您的customerArray是一个Optional,它的类型是AnyObject吗? (这是因为.valueForKey返回一个Optional)。你不能循环使用Optional。解决方案是在安全展开时将结果转换为数组:
if let customerArray = dataDictionary.valueForKey("kart") as? NSArray {
for js in customerArray {
let nameArray = js.valueForKey("name")
// ...
}
}
#2
0
It happend because valueForKey
method returns Optional
value of AnyObject
type (AnyObject?
). Indeed, AnyObject
has no Generator
member, and can't be used in for..in
loop (as well as an Optional
value). So, you should unwrap optional value and cast it to expected type, like this:
它发生是因为valueForKey方法返回AnyObject类型的Any值(AnyObject?)。实际上,AnyObject没有Generator成员,也不能用于for..in循环(以及一个Optional值)。因此,您应该解包可选值并将其转换为预期类型,如下所示:
let dataDictionary:NSDictionary = try NSJSONSerialization.JSONObjectWithData(responseObject as! NSData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
var customerArray = dataDictionary.valueForKey("kart")! as! [NSDictionary]
for js: NSDictionary in customerArray {
let nameArray = js.valueForKey("name") as! NSArray
}
#1
8
Your customerArray
is an Optional, it has the type AnyObject?
(this is because .valueForKey
returns an Optional). You can't loop over an Optional. Solution is to cast the result as an array while safe unwrapping:
您的customerArray是一个Optional,它的类型是AnyObject吗? (这是因为.valueForKey返回一个Optional)。你不能循环使用Optional。解决方案是在安全展开时将结果转换为数组:
if let customerArray = dataDictionary.valueForKey("kart") as? NSArray {
for js in customerArray {
let nameArray = js.valueForKey("name")
// ...
}
}
#2
0
It happend because valueForKey
method returns Optional
value of AnyObject
type (AnyObject?
). Indeed, AnyObject
has no Generator
member, and can't be used in for..in
loop (as well as an Optional
value). So, you should unwrap optional value and cast it to expected type, like this:
它发生是因为valueForKey方法返回AnyObject类型的Any值(AnyObject?)。实际上,AnyObject没有Generator成员,也不能用于for..in循环(以及一个Optional值)。因此,您应该解包可选值并将其转换为预期类型,如下所示:
let dataDictionary:NSDictionary = try NSJSONSerialization.JSONObjectWithData(responseObject as! NSData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
var customerArray = dataDictionary.valueForKey("kart")! as! [NSDictionary]
for js: NSDictionary in customerArray {
let nameArray = js.valueForKey("name") as! NSArray
}