Charles Xavier
Charles Xavier

Reputation: 1045

type nsfastenumerationiterator.element aka any has no subscript members

I've updated Xcode from 7 to 8 and Swift from 2.3 to 3.

I'm getting this error at let names = candidate["CandidateName"]!:

type nsfastenumerationiterator.element aka any has no subscript members

enter image description here

    let url = URL(string: "https://website.com")
    let data = try? Data(contentsOf: url!)
    var tmpValues = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
    tmpValues = tmpValues.reversed() as NSArray
    reloadInputViews()


    for candidate in tmpValues {
        if ((candidate as? NSDictionary) != nil) {
            let names = candidate["CandidateName"]!

            //self.values.append(candidate["CandidateName"])
            self.values.append(name!)
            print(name)

        }
    }

Upvotes: 7

Views: 3968

Answers (1)

Durul Dalkanat
Durul Dalkanat

Reputation: 7445

I think your for in loop should like this. This is work for me. But be sure var tmpValues.

for candidate in (tmpValues as? [[String:Any]])! {
     if ((candidate as? NSDictionary) != nil) {
         let names = candidate["CandidateName"]! as? String

         //self.values.append(candidate["CandidateName"])
         self.values.append(name!)
         print(name)

     }
 }

Upvotes: 10

Related Questions