Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type nsfastenumerationiterator.element aka any has no subscript members

Tags:

swift

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)

        }
    }
like image 803
Charles Xavier Avatar asked Sep 16 '16 15:09

Charles Xavier


1 Answers

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)

     }
 }
like image 178
Durul Dalkanat Avatar answered Nov 03 '22 19:11

Durul Dalkanat