Problems with Conversion of Array to Swift 3

Asked

Viewed 60 times

0

With the swift3 update I’m having the following error "type 'Any' has on the Members subscriber", I’ve seen a lot of questions on the subject and tested most of them but without success. The code where I’m making this mistake is as follows::

 Alamofire.request(mutableURLRequest)
                .responseJSON{
                    response in
                    if let value: AnyObject = response.result.value as AnyObject? {

                        let json = JSON(value)
                        for (index, subJson) in json {
                            self.addressArray.add(subJson.object)
                            let ind: Int = Int(index)!

                            let defaultvalue: Int = self.Array[ind]["IsDefault"] as? Int

                            if(defaultvalue == 1)
                            {
                                let prefs: UserDefaults = UserDefaults.standard
                                let ID: Int = self.Array[ind]["ID"] as! Int
                                let Name: String = self.Array[ind]["Tag"] as! String

                                prefs.set(addressName, forKey: "Name")
                                prefs.set(addressID, forKey: “ID")
                                prefs.synchronize()
                            }

                        }
                        self.listAddress.reloadData()
                    }
            }

Since the mistake is happening everywhere I do something like this self.Array[ind]["IsDefault"] as? Int.

1 answer

0

Change:

let json = JSON(value)

For:

let json = JSON(value) as! [String:AnyObject]

You have to cast a JSON for the types you will handle in the code.

  • I did what I said but I still have the same error on the self lines. Array[Ind]["Isdefault"] as? Int and now in the code you said appears the following warning "Cast from 'JSON' to unrelated type ''[String: Anyobject] Always fails"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.