Extract parameter
The Extract Parameter refactoring lets you extract a new parameter to a method.
Extract a parameter
In the editor, place the caret within the expression that you want to introduce as a parameter.
Press ⌃ ⌥ P 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 ⏎.
Type the name of the newly created parameter or select one of the suggested values.
If you want to declare the generated parameter as a constant, check Declare const in the popup that opens.
Press ⏎.
Code examples
Before | After |
---|---|
@implementation Greeting {
}
- (NSString *)generateGreeting{
// This expression will be converted to
// the method's parameter
NSString *userName = @"Bob";
return [NSString stringWithFormat:@"Hello %@", userName];
}
- (void)print {
// Method's call
NSLog(@"%@", [self generateGreeting]);
}
@end |
@implementation Greeting {
}
// Method with the new parameter
- (NSString *)generateGreeting:(NSString *)userName {
return [NSString stringWithFormat:@"Hello %@", userName];
}
- (void)print {
// Method's call
NSLog(@"%@", [self generateGreeting:@"Bob"]);
}
@end
|
Last modified: 17 March 2022