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条回答
倾城 Initia
2楼-- · 2019-01-03 14:54

For me this is working.

For GET Request

Alamofire.request("http://jsonplaceholder.typicode.com/todos/1/get").responseJSON { (response:DataResponse<Any>) in

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

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

        }

    }

For POST

let parameters = NSDictionary(object: "nara", forKey: "simha" as NSCopying)

    Alamofire.request("http://jsonplaceholder.typicode.com/posts", method: HTTPMethod.post, parameters: parameters as? Parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in


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

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

        }
    }

Thanks @Rajan Maheswari.

查看更多
Lonely孤独者°
3楼-- · 2019-01-03 14:57
func API()
{
    if Reachability.isConnectedToNetwork()
    {
        let headers = ["Vauthtoken":"Bearer \(apiToken)"]
        print(headers)
        //            let parameter = ["iLimit":"10","iOffset":"0","iThreadId":"1"]
        ApiUtillity.sharedInstance.showSVProgressHUD(text: "Loding...")
        Alamofire.request(ApiUtillity.sharedInstance.API(Join: "vehicle/CurrentVehicleLists"), method:.get, parameters:nil, headers: headers).responseJSON { response in
            switch response.result {
            case .success:
                print(response)
                ApiUtillity.sharedInstance.dismissSVProgressHUD()
                let dictVal = response.result.value
                let dictMain:NSDictionary = dictVal as! NSDictionary
                let statusCode = dictMain.value(forKey: "status") as! Int
                if(statusCode == 200)
                {

                }
                else if statusCode == 401
                {

                }
                else
                {

                }
            case .failure(let error):
                print(error)
                ApiUtillity.sharedInstance.dismissSVProgressHUD()
            }
        }
    } else
    {
        ApiUtillity.sharedInstance.dismissSVProgressHUD()
        ApiUtillity.sharedInstance.showErrorMessage(Title: "Internet Connection", SubTitle: "Internet connection Faild", ForNavigation: self.navigationController!)
    }
}
查看更多
Ridiculous、
4楼-- · 2019-01-03 14:58

I fixed this issue with:

  1. Reorder parameters (url then method type).
  2. Change Encoding Enum to be "JSONEncoding.default" for example.

Note that: Alamofire methods signature change in Swift 3

查看更多
ら.Afraid
5楼-- · 2019-01-03 15:02

Two things that I found worth noting.

  1. Remove the first url label before its value. Use Alamofire.request("https://yourServiceURL.com", method: .post, instead of Alamofire.request(url: "https://yourServiceURL.com", method: .post,.
  2. Make sure the data type of the parameters is [String: String]. Declare it explicitly.
查看更多
仙女界的扛把子
6楼-- · 2019-01-03 15:03

Post method Alamofire 4.0 with Swift 3.0 and xCode 8.0

Alamofire.request(URL, method: .post, parameters: PARAMS)
                            .responseJSON { closureResponse in
                        if String(describing: closureResponse.result) == "SUCCESS"
                        { 
                           // Sucess code  
                        }
                        else
                        { 
                           // Failure Code 
                        }
                 }
查看更多
Root(大扎)
7楼-- · 2019-01-03 15:07

This error is up to parameters value. It has to be [String: String]

let url = URL(string: "http://yourURLhere")!

    let params: [String: String] = ["name": "oskarko", "email": "youremail@here.com", "sex": "male"]



    Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.default, headers: nil).validate(statusCode: 200..<600).responseJSON() { response in

        switch response.result {
        case .success:

            var result = [String:String]()

            if let value = response.result.value {

                let json = JSON(value) 

            }

        case .failure(let error):
            print("RESPONSE ERROR: \(error)")

        }

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