AppCode 2021.1 Help

Extract closure

The Extract Closure refactoring creates a closure out of the selected chunk of code.

Extract closure

Extract a closure

  1. In the editor, select an expression which you want to extract into a closure.

  2. Press ⌃⌥⇧T and select Extract Closure. Alternatively, select Refactor | Extract/Introduce | Closure from the main or context menu.

  3. If there are several expressions available for extracting, select the required one from the list that opens and press .

  4. In the dialog that opens, type the name of the new closure and change the parameter names and types if necessary.

  5. Press .

Code example

BeforeAfter
private func performLogin() { let email = self.textFieldEmail.text ?? "" let isEmailValid = self.emailValidator.isEmailValid(email: email) // To be extracted textFieldEmail.textColor = isEmailValid ? UIColor.black : UIColor.red }
private func performLogin() { let email = self.textFieldEmail.text ?? "" let isEmailValid = self.emailValidator.isEmailValid(email: email) // Extracted closure let setFiledStyle = { (isEmailValid: Bool) in self.textFieldEmail.textColor = isEmailValid ? UIColor.black : UIColor.red } setFiledStyle(isEmailValid) }
    Last modified: 08 April 2021