I am trying to make a POST request in Alamofire to return a JSON object. This code worked in Swift 1, but in swift 2 I get this invalid parameter issue:
Tuple types '(NSURLRequest?, NSHTTPURLResponse?, Result<AnyObject>)' (aka '(Optional<NSURLRequest>, Optional<NSHTTPURLResponse>, Result<AnyObject>)') and '(_, _, _, _)' have a different number of elements (3 vs. 4)
It seems like the error parameter was removed, but I am using the error parameter inside the function to check for errors, so how would I do that without an error param?
Here's my code for the POST request:
let response = Alamofire.request(.POST, urlPath, parameters: parameters, encoding: .URL)
.responseJSON { (request, response, data, error) in
if let anError = error
{
// got an error in getting the data, need to handle it
print("error calling POST on /posts")
print(error)
}
else if let data: AnyObject = data
{
// handle the results as JSON, without a bunch of nested if loops
let post = JSON(data)
// to make sure it posted, print the results
print("The post is: " + post.description)
}
}
Have you tried using three parameters in the .responseJSON and inserting a try catch block around the areas you want error logging, check out this link if you need help with swift 2 try catchs
If you see the documentation in the branch
Swift2.0
you can see that theresponseJSON
function has changed as the error says, it have now three parameters but you can catch the error too, lets take a look:Now it returns an
enum
Result<AnyObject>
and according to the doc :And it have inside an property entitled
error
, with the following doc:Then if you follow this test case inside Alamofire you can see how to get the error properly:
Alamofire 3.0.0 introduces a
Response
struct. All response serializers (with the exception ofresponse
) return a genericResponse
struct.So you can call it like the following way:
You can read more about the migration process in the Alamofire 3.0 Migration Guide.
I hope this help you.