JSON Object with Array. Migration from Swift 2 to Swift 3

Asked

Viewed 48 times

0

Previously in my app when I made a call to the server and if there was an error I would do the following:

Alamofire.request(mutableURLRequest).responseJSON{
response in if let JSON = response.result.value{
if JSON.count != 0{
let errorList = JSON["responseErrorsList"] as? NSArray
 for error in errorList!{
   let erro: String = error as! String
     switch erro{
       case "EntityRequired":

with the migration from Swift 2 to Swift 4 I am having problems since errosList comes to nil however the result of JSON is as follows:

["Rate": "Level": "Code": "ID": 0, "Zone": "Address": "Errorslistserver": <__Nssingleobjectarrayi 0x17400ba90>( Invalidcode ) , "Subzone": ]

Does anyone know how to access the "Errorslistserver" field and get the errors that in this case is "Invalidcode".

What I have implemented and is not working is as follows::

if let JSON = response.result.value as? [String: Any]{
    if (JSON as AnyObject).count != 0{
      let errorList = JSON["responseErrorsList"] as? [[String: Any]]

1 answer

0

I found the answer to my question:

if let JSON = response.result.value as? [String: Any], 
let errorList = JSON["ErrorsListServer"] as? [String] {
  for error in errorList {
      print(error)
  }
}

Browser other questions tagged

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