0

I was extracting closure to method but I always get this error:

Function is unused

This is whole working func:

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

This is how I extracted last closure to method:

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

If I try this, the error "Function is unused" shows up:

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

Edit:

Here is alert method that is used:

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

If I add back alert action button it works:

let okButton = UIAlertAction(
            title: "OK",
            style: .default,
            handler:startOver())
4
  • You are calling the startOver() function that returns another function of type (UIAlertAction) -> Void, but you are not using it. Did you try replacing that line with let _ = self?.startOver()? Commented Mar 10, 2022 at 19:41
  • Try self?.startOver()() Commented Mar 10, 2022 at 19:48
  • Yes I've tried both _ = self?.startOver and let _ = self?.startOver(If there are any differences) But my tests still fail. @HunterLion Commented Mar 10, 2022 at 19:51
  • @aheze I just got new error about inserting alertAction If I do that. "Missing argument for parameter #1 in call" Commented Mar 10, 2022 at 19:55

1 Answer 1

0

You're currently calling that function inside of the action you pass to your showAlert function but then throwing away the action it returns. Instead, you want to pass the action that it returns directly to your showAlert method rather than wrapping it inside another action with the trailing closure syntax:

self?.showAlert(message: message, okAction: self!.startOver())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.