重构后闭包内的函数未被使用

After refactoring function inside closure is unused

我正在提取方法的闭包,但我总是得到这个错误:

函数未使用

这是全部工作功能:

        fileprivate func attemptToChangePassword() {
    
    passwordChanger.change(securityToken: securityToken, oldPassword: oldPassword.text ?? "", newPassword: newPassword.text ?? "", onSuccess:{[weak self] in
        self?.hideSpinner()
        let alertController = UIAlertController(
            title: nil,
            message: "Your password has been successfully changed.",
            preferredStyle: .alert)
        let okButton = UIAlertAction(
            title: "OK",
            style: .default) { [weak self] _ in
                self?.dismiss(animated: true)
            }
        alertController.addAction(okButton)
        alertController.preferredAction = okButton
        self?.present(alertController, animated: true)
        
    }, onFailure: {[weak self] message in
        self?.hideSpinner()
        self?.showAlert(message: message) { [weak self] _ in
            self?.oldPassword.text = ""
            self?.newPassword.text = ""
            self?.confirmPassword.text = ""
            self?.oldPassword.becomeFirstResponder()
            self?.view.backgroundColor = .white
            self?.blurView.removeFromSuperview()
            self?.cancel.isEnabled = true
        }
            
    })
}

这就是我提取方法最后一个闭包的方式:

fileprivate func startOver() -> (UIAlertAction) -> Void {
    return { [weak self] _ in
        self?.oldPassword.text = ""
        self?.newPassword.text = ""
        self?.confirmPassword.text = ""
        self?.oldPassword.becomeFirstResponder()
        self?.view.backgroundColor = .white
        self?.blurView.removeFromSuperview()
        self?.cancel.isEnabled = true
    }
}

如果我尝试这样做,会出现错误 “功能未使用”

  onFailure: { [weak self] message in
 self?.hideSpinner()
 self?.showAlert(message: message) { [weak self] _ in
  self?.startOver()//FUNCTION IS UNUSED
  }
 })

编辑:

这里是使用的警报方法:

      fileprivate func showAlert( message: String, okAction: @escaping (UIAlertAction) -> Void) {
    let ac = UIAlertController(title: nil, message: message, preferredStyle: .alert)
    let ok = UIAlertAction(title: "OK", style: .default, handler:okAction)
    ac.addAction(ok)
    ac.preferredAction = ok
    self.present(ac, animated: true)
}

如果我添加返回警报操作按钮,它会起作用:

let okButton = UIAlertAction(
            title: "OK",
            style: .default,
            handler:startOver())

您当前在传递给 showAlert 函数的操作中调用该函数,但随后丢弃了它 returns 的操作。相反,您想将 returns 的操作直接传递给您的 showAlert 方法,而不是使用尾随闭包语法将其包装在另一个操作中:

self?.showAlert(message: message, okAction: self!.startOver())