Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire Swift 3.0 Extra argument in call

I have migrated my project to Swift 3 (and updated Alamofire to latest Swift 3 version with pod 'Alamofire', '~> 4.0' in the Podfile).

I now get an "Extra argument in call" error on every Alamofire.request. Eg:

let patientIdUrl = baseUrl + nextPatientIdUrl
Alamofire.request(.POST, patientIdUrl, parameters: nil, headers: nil, encoding: .JSON)

Can anybody tell me why ?

like image 201
Agreensh Avatar asked Sep 14 '16 12:09

Agreensh


2 Answers

According to Alamofire documentation for version 4.0.0 URL request with HTTP method would be followings:

Alamofire.request("https://httpbin.org/get") // method defaults to `.get`    
Alamofire.request("https://httpbin.org/post", method: .post)
Alamofire.request("https://httpbin.org/put", method: .put)
Alamofire.request("https://httpbin.org/delete", method: .delete)

So your url request will be:

Alamofire.request(patientIdUrl, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: nil)

and a sample request will be:

Alamofire.request(url, method: .post, parameters: param, encoding: JSONEncoding.default, headers: [AUTH_TOKEN_KEY : AUTH_TOKEN])
    .responseJSON { response in
        print(response.request as Any)  // original URL request
        print(response.response as Any) // URL response
        print(response.result.value as Any)   // result of response serialization
}

Hope this helps!

like image 174
Abdullah Md. Zubair Avatar answered Oct 20 '22 23:10

Abdullah Md. Zubair


This one worked for me.
No need to remove encoding parameter

Update for Swift 5.x

Alamofire uses the Result type introduced in Swift 5.
Also Alamofire.request has been changed to AF.request which will now read their switch response.result value with .success and .failure

AF.request("https://yourServiceURL.com", method: .get, parameters: [:], encoding: URLEncoding.default, headers: ["":""]).responseJSON { (response) in
        switch response.result {
        case let .success(value):
            print(value)
        case let .failure(error):
            print(error)
    }
}

Swift 3.x / 4.x

Alamofire.request("https://yourServiceURL.com", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case .success(_):
            if let data = response.result.value{
                print(response.result.value)
            }
            break

        case .failure(_):
            print(response.result.error)
            break

        }
    }

and make sure that the parameters are of type

[String:Any]?

In case of Get

Alamofire.request("https://yourGetURL.com", method: .get, parameters: ["":""], encoding: URLEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case .success(_):
            if let data = response.result.value{
                print(response.result.value)
            }
            break

        case .failure(_):
            print(response.result.error)
            break

        }
    }

Even works with

JSONEncoding.default 

For Headers

If you are passing headers, make sure their type should be [String:String]

Go through the Parameter Encoding Link https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md#parameter-encoding-protocol

like image 71
Rajan Maheshwari Avatar answered Oct 20 '22 23:10

Rajan Maheshwari