Extra argument 'method' in call

2019-01-23 22:16发布

Getting error while calling Alamofire request method in the latest version(4.0.0).

The syntax is:

Alamofire.request(urlString,method: .post, parameters: requestParams, encoding: .JSON, headers: [:])

the type of requestParam is [String:Any]

13条回答
We Are One
2楼-- · 2019-01-23 22:58

My problem was in optional parameters in the headers that were not unwrapped.

查看更多
该账号已被封号
3楼-- · 2019-01-23 23:01

It means that some of the parameters type are wrong, check that you are sending these values:

url: String
method: HTTPMethod  (E.g: .post)
parameters: [String:Any]
encoding: ParameterEncoding  (E.g: JSONEncoding.default)
headers: [String: String]
查看更多
混吃等死
4楼-- · 2019-01-23 23:04

You are getting that error because of the wrong data types.

Parameters Type should be [String : Any] and parameters type shouldn't be an optional.

Header Type should be [String : String] or nil and header type shouldn't be an optional as well.

Hope it helps. :-)

查看更多
冷血范
5楼-- · 2019-01-23 23:05

Make sure your parameters is [String: Any]

i.e

let parameters = ["foo": "bar"]

Not:

let parameters : Parameter = ["foo": "bar"]
查看更多
Evening l夕情丶
6楼-- · 2019-01-23 23:07

I can only refer you to: https://github.com/Alamofire/Alamofire/issues/1508#issuecomment-246207682

Basically, if one of your parameters is of the wrong type, the swift compiler will assume you're using request(urlRequest:URLRequestConvertible) and then, the method is an extra argument

Go over that parameters again and make sure all is of correct type (Parameters?, ParameterEncoding, and HTTPHeaders)

查看更多
一夜七次
7楼-- · 2019-01-23 23:07

Updated for Swift 3:

let requestString = "https://thawing-inlet-46474.herokuapp.com/charge.php"
        let params = ["stripeToken": token.tokenId, "amount": "200", "currency": "usd", "description": "testRun"]

        Alamofire.request(requestString,method: .post, parameters: params, encoding: JSONEncoding.default, headers: [:]).responseJSON { (response:DataResponse<Any>) in

            switch(response.result) {
            case .success(_):
                if response.result.value != nil{
                    print("response : \(response.result.value)")
                }
                break

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

            }
        }
查看更多
登录 后发表回答