Search code examples
xcodeswiftalamofireproperty-list

Alamofire Response Property List


I'm going through a tutorial and am attempting to make an alamofire request to a property list. In the closure for the response property list, I use the arguments (_, _, result). However, XCode gives me the error:

Cannot convert value of type '(_, _, _) -> Void' to expected argument type 'Response -> Void'

I am using alamofire 3.0 beta.


Solution

  • Alamofire right now is in version 3.3 according to the releases in the repository, since the version 3.0 it has change a little.

    In you use the Response Handler your closure need to look like this:

    Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
         .response { request, response, data, error in
             print(request)
             print(response)
             print(data)
             print(error)
          }
    

    And if you use for example the Response JSON Handler everything is encapsulated now in the response like in this code:

    Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
         .responseJSON { response in
             print(response.request)  // original URL request
             print(response.response) // URL response
             print(response.data)     // server data
             print(response.result)   // result of response serialization
    
             if let JSON = response.result.value {
                 print("JSON: \(JSON)")
             }
         }
    

    Or you can use this code for more easily handling:

    Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
         .responseJSON { response in
             switch(response.result) {
             case .Success(let value):
                if let JSON = value {
                  print("JSON: \(JSON)")
                }
             case .Failure(let error):
                print(error.description)    
             }
         } 
    

    I hope this help you.