Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift For Loop Value of type 'AnyObject?' has no member 'Generator'

I am trying to do this but it's says

Value of type 'AnyObject?' has no member 'Generator'

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.

like image 616
Murat Kaya Avatar asked Nov 06 '15 12:11

Murat Kaya


1 Answers

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:

if let customerArray = dataDictionary.valueForKey("kart") as? NSArray {
    for js in customerArray {
        let nameArray = js.valueForKey("name")
        // ...
    }
}
like image 186
Eric Aya Avatar answered Sep 30 '22 11:09

Eric Aya