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条回答
老娘就宠你
2楼-- · 2019-01-23 22:45

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

查看更多
虎瘦雄心在
3楼-- · 2019-01-23 22:45

I was facing same problem And try with all answer as previously post here, And then I got the solution and reason of this problem .

This is happened due to pass the wrong object parse in the request, and finally the solution --

theJSONText -- JSON string

urlStr -- URL string

 let urlEncoadedJson = theJSONText.addingPercentEncoding(withAllowedCharacters:.urlHostAllowed)
    let urls = NSURL(string:"urlStr\(urlEncoadedJson ?? "")")

let method: HTTPMethod = .get

Alamofire.request(urls! as URL, method: method, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case .success(_):

            if response.result.value != nil
            { 
              // write your code here
            }
            break

        case .failure(_):
            if response.result.error != nil
            {
                print(response.result.error!) 
            }
            break
        }

    }

Note - There is no param in my URL .

查看更多
Animai°情兽
4楼-- · 2019-01-23 22:52

I was having the same issue, the problem is at parameters type, it should be of type [String: Any]. After I made this change, it worked for me.

 Alamofire.request(youUrl, method: .post, parameters: param as? [String: Any], encoding: JSONEncoding.default, headers: [:])
                .responseJSON { response in
查看更多
Anthone
5楼-- · 2019-01-23 22:54

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)

查看更多
三岁会撩人
6楼-- · 2019-01-23 22:55

This is a family of functions that are very similar, which makes the compiler think you're trying to use a different function. Double check that ALL of the arguments you're supplying are the CORRECT TYPE that is expected. For example I was passing [String:Any] for the variant with headers, which expected [String:String] and got the same error.

查看更多
SAY GOODBYE
7楼-- · 2019-01-23 22:56

Almofire methods changed in Swift 3 as the following:

  1. Reorder parameters (url then method type).
  2. Change Encoding Enum to be "JSONEncoding.default" for example.
查看更多
登录 后发表回答