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.
Perform the Extract Instance Variable refactoring
In the editor, select an expression you want to replace with an instance variable.
Press Ctrl+Alt+F or select
from the main or context menu.If there are several expressions available for extracting, select the required one from the list that opens and press Enter.
Select a name for the instance variable from the list of suggestions that opens or type a new one.
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.
Press Enter.
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: 14 December 2022