AppCode 2021.2 Help

Extract parameter

The Extract Parameter refactoring lets you extract a new parameter to a method.

Extract parameter

Extract a parameter

  1. In the editor, place the caret within the expression that you want to introduce as a parameter.

  2. Press ⌃ ⌥ P or select Refactor | Extract/Introduce | Parameter 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. Type the name of the newly created parameter or select one of the suggested values.

  5. If you want to declare the generated parameter as a constant, check Declare const in the popup that opens.

  6. Press .

Code examples

BeforeAfter
@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: 08 March 2021