Alamofire POST request with Swift 2

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 got this invalid problem:

Tuple types '(NSURLRequest?, NSHTTPURLResponse?, Result<AnyObject>)' (aka '(Optional<NSURLRequest>, Optional<NSHTTPURLResponse>, Result<AnyObject>)') and '(_, _, _, _)' have a different number of elements (3 vs. 4) 

It looks like the error parameter has been deleted, but I use the error parameter inside the function to check for errors, so how would I do this without the error parameter?

Here is 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) } } 
+6
source share
2 answers

If you see the documentation in the Swift2.0 branch, you can see that the responseJSON function has been changed, as the error says, now it has three parameters, but you can also catch the error, let's take a look:

 public func responseJSON( options options: NSJSONReadingOptions = .AllowFragments, completionHandler: (NSURLRequest?, NSHTTPURLResponse?, Result<AnyObject>) -> Void) -> Self { return response( responseSerializer: Request.JSONResponseSerializer(options: options), completionHandler: completionHandler ) } 

Now it returns enum Result<AnyObject> and according to the doc:

 Used to represent whether a request was successful or encountered an error. - Success: The request and all post processing operations were successful resulting in the serialization of the provided associated value. - Failure: The request encountered an error resulting in a failure. The associated values are the original data provided by the server as well as the error that caused the failure. 

And it has a property called error with the following document:

 /// Returns the associated error value if the result is a failure, `nil` otherwise. public var error: ErrorType? { switch self { case .Success: return nil case .Failure(_, let error): return error } } 

Then, if you run this test inside Alamofire, you can see how to get the error correctly:

 func testThatResponseJSONReturnsSuccessResultWithValidJSON() { // Given let URLString = "https://httpbin.org/get" let expectation = expectationWithDescription("request should succeed") var request: NSURLRequest? var response: NSHTTPURLResponse? var result: Result<AnyObject>! // When Alamofire.request(.GET, URLString, parameters: ["foo": "bar"]) .responseJSON { responseRequest, responseResponse, responseResult in request = responseRequest response = responseResponse result = responseResult expectation.fulfill() } waitForExpectationsWithTimeout(defaultTimeout, handler: nil) // Then XCTAssertNotNil(request, "request should not be nil") XCTAssertNotNil(response, "response should not be nil") XCTAssertTrue(result.isSuccess, "result should be success") XCTAssertNotNil(result.value, "result value should not be nil") XCTAssertNil(result.data, "result data should be nil") XCTAssertTrue(result.error == nil, "result error should be nil") } 

UPDATE

Alamofire 3.0.0 introduces a Response framework. All response serializers (except Response ) return a generic Response construct.

 public struct Response<Value, Error: ErrorType> { /// The URL request sent to the server. public let request: NSURLRequest? /// The server response to the URL request. public let response: NSHTTPURLResponse? /// The data returned by the server. public let data: NSData? /// The result of response serialization. public let result: Result<Value, Error> } 

So you can call it like this:

 Alamofire.request(.GET, "http://httpbin.org/get") .responseJSON { response in debugPrint(response) } 

You can learn more about the migration process in the Alamofire 3.0 Migration Guide .

Hope this helps you.

+9
source

Have you tried using three parameters in .responseJSON and inserting a catch try block around the areas in which you want to log errors, check this link if you need help with quick 2 try catchs

+2
source

Source: https://habr.com/ru/post/987088/


All Articles