Extract a Method with Parameters

You may have noticed that ChangePasswordViewController has a lot of repeated code. In the newly extracted validateInputs method, find the first alert code shown here:

 if​ newPasswordTextField.text?.isEmpty ?? ​true​ {
 let​ alertController = ​UIAlertController​(
  title: ​nil​,
  message: ​"Please enter a new password."​,
  preferredStyle: .alert)
 let​ okButton = ​UIAlertAction​(
  title: ​"OK"​,
  style: .​default​) { [​weak​ ​self​] _ ​in
 self​?.newPasswordTextField.​becomeFirstResponder​()
  }
  alertController.​addAction​(okButton)
  alertController.preferredAction = okButton
 self​.​present​(alertController, animated: ​true​)
 return ...

Get iOS Unit Testing by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.