How to get the result value of Alamofire.request().responseJSON in swift 2?

Edgar Georgel

I have a question about the new version of Alamofire for Swift 2

Alamofire.request(.POST, urlString, parameters: parameters as? [String : AnyObject])
        .responseJSON { (request, response, result) -> Void in
            let dico = result as? NSDictionary
            for (index, value) in dico! {
                print("index : \(index)     value : \(value)")
            }
    }

In this section I would like to cast the result in to a NSDictionary. But When I compile and put a breakpoint, the debugger says that dico is nil. If I use debugDescription to print result, it is not nil and contains what I expected How can I cast the Result variable?

Javier Cadiz

The accepted answer works great but with the introduction of Alamofire 3.0.0 there are some breaking changes that affects this implementation.
The migration guide has further explanations but i will highlight the ones related to the actual solution.

  • Response
    All response serializers (with the exception of response) return a generic Response struct.

  • Response type
    The Result type has been redesigned to be a double generic type that does not store the NSData? in the .Failure case.

Also take in count that Alamofire treats any completed request to be successful, regardless of the content of the response. So you need to chain a .validate() before .responseJSON() to hit the .Failure case. Read more about it here.

Updated code:

let url = "http://api.myawesomeapp.com"
Alamofire.request(.GET, url).validate().responseJSON { response in
    switch response.result {
    case .Success(let data):
        let json = JSON(data)
        let name = json["name"].stringValue
        print(name)
    case .Failure(let error):
        print("Request failed with error: \(error)")
    }
}

For reference:

  • Xcode 7.3 (Swift 2.2)
  • Alamofire 3.3.1
  • SwiftyJSON 2.3.3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to obtain value of JSON array as a result of get Request?

Value of type 'Result<String>' has no member 'error' [Alamofire, Swift 5]

Alamofire POST request with Swift 2

Alamofire 2, how to get NSError?

Get JSON result with GET request and parameters with Alamofire

Sending json object in GET request in swift in Alamofire

the difference between responseJSON and responseData of Alamofire

Mappting Alamofire responseJSON via ObjectMapper

How to POST request with parameters using Alamofire/URLSession.dataTask in swift

How to convert CURL request to Swift using Alamofire?

Swift Alamofire HTTP Get Request

Migrating Alamofire.Request extension from Swift 2 to Swift 3

Swift: Alamofire Value of type 'Self' (Request) has no member 'responseJSON' extension

How to request get value?

How to get a random value of a dictionary in swiftyJSON and Alamofire

responseJSON json parse in Alamofire

AlamoFire: How can get responseJSON values

Why can't I get the request result form Alamofire

AlamoFire responseJSON decimal precision

alamofire get request return null value

Swift Alamofire POST request becomes GET

get value of nested array alamofire swift 3

How to pass JSON object in Alamofire request swift 3

How to return respond result to function with Alamofire Swift 3

Swift 3: How to add or remove parameters in Request Body in ALamofire 4?

Get responseJson from JavaScript result

Parse Alamofire result swift

Cannot get the POST request to work AlamoFire and XCode/Swift

How to decodable request from Laravel in Swift and Alamofire