Extra argument 'method' in call

前端 未结 15 1685
你的背包
你的背包 2020-12-16 08:59

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

The syntax is:

Alamofire.request(urlString,method: .post, parame         


        
相关标签:
15条回答
  • 2020-12-16 09:25

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

    0 讨论(0)
  • 2020-12-16 09:27

    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)

    0 讨论(0)
  • 2020-12-16 09:28

    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]
    
    0 讨论(0)
  • 2020-12-16 09:36

    It is always because im using optional variables in any of the parameters

    0 讨论(0)
  • 2020-12-16 09:38

    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
    
                }
            }
    
    0 讨论(0)
  • 2020-12-16 09:39

    I fixed this by ensuring my requestUrls are strings, and not URL types. Also I removed parameter arguments for when parameter was nil.

    E.g.

    Alamofire.request(requestUrl, method: .get, encoding: URLEncoding.default)

    0 讨论(0)
提交回复
热议问题