AppCode 2022.2 Help

Extract instance variable

With the Extract Instance Variable refactoring, you can place an expression into an instance variable.

The refactoring is available for Objective-C only.

Extract instance variable

Perform the Extract Instance Variable refactoring

  1. In the editor, select an expression you want to replace with an instance variable.

  2. Press ⌃ ⌥ F or select Refactor | Extract/Introduce | Instance Variable 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. Select a name for the instance variable from the list of suggestions that opens or type a new one.

  5. If you want to create a property for the variable or declare it in the interface, select the corresponding checkboxes in the popup that opens.

  6. Press .

Code example

Before

After

@implementation LoginViewController { } - (void)performLogin { // ... [self.loginModel loginWithEmail:email password:password completion:^(BOOL success) { // success will be extracted to // an instance variable if (success) { [self showAlertLoginSuccess]; } else { [self showAlertLoginFailed]; } }]; }
@implementation LoginViewController { // Extracted instance variable BOOL _loginSuccessful; } - (void)performLogin { // ... [self.loginModel loginWithEmail:email password:password completion:^(BOOL success) { _loginSuccessful = success; if (_loginSuccessful) { [self showAlertLoginSuccess]; } else { [self showAlertLoginFailed]; } }]; }
    Last modified: 26 August 2021