Alamofire Swift 3.0 Extra argument in call

2019-01-03 14:45发布

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 ?

14条回答
啃猪蹄的小仙女
2楼-- · 2019-01-03 14:46

If you have added Alamofire files locally then don't use "Alamofire" before request

let apipath = “your api URL”    
    request(apipath, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { response in switch(response.result) {
            case .success(_):
                do {
                    let JSON = try JSONSerialization.jsonObject(with: response.data! as Data, options:JSONSerialization.ReadingOptions(rawValue: 0))

                    guard let JSONDictionary: NSDictionary = JSON as? NSDictionary else {
                        print("Not a Dictionary")
                        return
                    }

                    print("Post Response : \(JSONDictionary)")
                }
                catch let JSONError as NSError {
                    print("\(JSONError)")
                }
                break
            case .failure(_):
                print("failure Http: \(String(describing: response.result.error?.localizedDescription))")
                break
            }
    }
查看更多
孤傲高冷的网名
3楼-- · 2019-01-03 14:47

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

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

查看更多
走好不送
4楼-- · 2019-01-03 14:48

I ran into this same Extra argument 'method' in call error when my URL variable was out of scope.

In your case, please make sure both baseUrl and nextPatientIdUrl are in scope when they are being used Alamofire.request(patientIdUrl,..) method.

Hopefully this resolves your issue. Thanks You!

查看更多
够拽才男人
5楼-- · 2019-01-03 14:49

I just resolved the same problem as you have. The problem is I have imported Alamofire in the header, so I just remove the Alamofire when call request. Like that:

request(.POST, patientIdUrl, parameters: nil, headers: nil, encoding: .JSON)

I hope it can help you.

查看更多
小情绪 Triste *
6楼-- · 2019-01-03 14:51

I fixed this issue this way:

Just remove extra parameters, just parameters, encoding and headers, if these parameters are nil you can remove then and leave this way,

Alamofire.request(yourURLString, method: .post)
查看更多
再贱就再见
7楼-- · 2019-01-03 14:52

I copy this code from Alamofire,create a URLRequest and used Alamofire.request(URLRequest) method, avoid this error

originalRequest = try URLRequest(url: url, method: method, headers: headers)
let encodedURLRequest = try encoding.encode(originalRequest!, with: parameters)
查看更多
登录 后发表回答