【问题标题】:Extra argument in call Alamofire Swift调用 Alamofire Swift 中的额外参数
【发布时间】:2023-03-21 16:14:01
【问题描述】:

当我调用 Alamofire 时,我一直在尝试通过修复参数类型、将响应类型从 responseString 更改为 responseJSON 和强制展开变量来解决此错误。我检查了以下答案,但没有运气:

Alamofire, Extra argument 'method' in call

Extra argument 'method' in call of Alamofire

Swift - Extra Argument in call

Swift 3.0, Alamofire 4.0 Extra argument 'method' in call

这是我的代码:

 func checkServerForLogin(email: String, password: String) {

    let parameters = [
        "email": email,
        "password": password
        ] as [String : Any]

    Alamofire.request(URL_CHECK_LOGIN, method: .post, parameters: parameters).responseString { (response) in

        if response.result.error == nil {

            guard let data = response.data else {
                return
            }
            do {
                print("LOGIN_RESULT")
                print(response)

            } catch {
                print("ERROR: \(error)")
            }
        } else {
            debugPrint(response.result.error as Any)
        }
    }
}

那我就叫它……

AuthService.instance.checkServerForLogin(email: email_input, password: password_input) { response, error in

            if ((response) != nil){

            }
        }

我一直收到Extra argument 'password' in call。任何解决此问题的帮助将不胜感激。

【问题讨论】:

  • checkServerForLogin() 不是 Alamofire 中的功能,据我所知。
  • 嘿,我做了一个单例类,AuthService,然后通过那里调用函数。不过谢谢:)
  • 您的方法声明 checkServerForLogin(email: String, password: String) 似乎只有两个参数,而在函数调用中,您使用了三个参数,即 emailpassword 和一个转义闭包参数
  • 你的函数没有block参数,所以你有这个错误,它与Alamofire无关,只是你的函数

标签: ios swift alamofire


【解决方案1】:

你已经创建了简单的方法。你需要创建完成块参数

试试这个代码

  class func checkServerForLogin(_ url:String,email: String, password: String,success:@escaping (JSON) -> Void, failure:@escaping (Error) -> Void) {

            let parameters = [
                "email": email,
                "password": password
                ] as [String : Any]

            Alamofire.request(url, method: .post, parameters: parameters).responseString { (response) in

                if response.result.isSuccess {
                    let resJson = JSON(response.result.value!)
                    success(resJson)
                }
                if response.result.isFailure {
                    let error : Error = response.result.error!
                    failure(error)
                }
            }
        }

AuthService.checkServerForLogin(URL_CHECK_LOGIN, email: email_input, password: password_input, success: { (responseObject) in
            print(responseObject)
        }) { (error) in
            print(error.localizedDescription)
        }

【讨论】:

    猜你喜欢
    • 2018-08-26
    • 2017-01-22
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 2017-11-26
    • 1970-01-01
    相关资源
    最近更新 更多