代码之家  ›  专栏  ›  技术社区  ›  Hews

call Alamofire Swift中的额外参数

  •  0
  • Hews  · 技术社区  · 6 年前

    我一直试图解决这个错误,当我调用ALAMOFE通过固定参数类型,改变响应类型从 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 . 如果能帮助解决这个问题,我们将不胜感激。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Jigar    6 年前

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

    尝试此代码

      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)
            }