AppCode 2019.2 Help

Extract

Extract variable

If you come across an expression that is hard to understand, or it is duplicated in several places throughout you code, you can place the result of such expression or its part into a separate variable using the Extract Variable refactoring.

  1. In the editor, select an expression or its part that you want to extract. You can also place the caret within the expression. In this case AppCode offers you a list of potential code selections.

  2. Press ⌥⌘V or select Refactor | Extract | Extract Variable from the main menu.

  3. Select a name for the variable from the list of suggestions that opens or type a new one.

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

  5. If necessary, use additional options:
    • For Swift: check Declare with 'var' if you want to declare a variable (with var). Uncheck this checkbox to declare a constant (with let). Check Specify type explicitly if you want to specify a data type in the declaration.

    • For Objective-C: check Declare const to declare a new reference as a constant (with const).

  6. Press .

Below you can see the result of the Extract Variable refactoring:

Before

After

- (void)method{ int a = 1; int b = a + self.getInt; }
- (void)method{ int a = 1; int *number = a + self.getInt; int b = number; }

Before

After

func method() { var a = 1 var b = a + getInt() }
func method(){ var a = 1 var number = a + getInt() var b = number }

Extract closure

The Extract Closure refactoring helps you create a closure based on the selected chunk of code. The refactoring is available for Swift only.

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

  2. Press ⌃T and select Closure. Alternatively, select Refactor | Extract | 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.

    Extract closure

  5. Press .

Below you can see the result of the Extract Closure refactoring:

Before

After

func method() { var a = 1 var b = 2 var c = a + b // extract to closure }
func method() { var a = 1 var b = 2 let sum = { (a: Int, b: Int) -> Int in return a + b } var c = sum(a, b) }

Extract constant

The Extract Constant refactoring helps you avoid using hardcoded constants without any explanations about their values or purpose.

  1. In the editor, select an expression or declaration of a variable you want to replace with a constant.

  2. Press ⌥⌘C to introduce a constant or select Refactor | Extract | Constant 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 constant from the list of suggestions that opens or type a new one.

  5. Uncheck the Put to header checkbox if you don't want the constant to be moved to the header file.

  6. Uncheck the Declare static checkbox if you don't want the constant to be declared as static.

  7. Press .

Extract instance variable

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

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

  2. Press ⌥⌘F or select Refactor | Extract | Instant 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 .

Below you can see the result of the Extract Instance Variable refactoring:

Before

After

@implementation MyClass { } - (void)method { int a = 1; int b = a + self.getInt; } - (int)getInt { return arc4random_uniform(100); } @end
@implementation MyClass { int _number; } - (void)method { int a = 1; _number = a + self.getInt; int b = _number; } - (int)getInt { return arc4random_uniform(100); } @end

Extract property

The Extract Property refactoring lets you move expressions and local declarations to properties.

  1. In the editor, select the expression or declaration you want to replace with a property.

  2. Press ⌥⌘E or select Refactor | Extract | Property 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 property from the list of suggestions that opens or type a new one.

  5. If you want the property to be generated in a private category, select the Put to private category checkbox. Otherwise, the property will be declared in the header file.

  6. Press .

Below you can see the result of the Extract Property refactoring:

Before

After

@implementation MyClass { } - (void)method { int a = 1; int b = a + self.getInt; } - (int)getInt { return arc4random_uniform(100); } @end
@implementation MyClass { } - (void)method { int a = 1; self.number = a + self.getInt; int b = self.number; } - (int)getInt { return arc4random_uniform(100); } @end

Extract parameter

The Extract Parameter refactoring is used to add a new parameter to a method declaration and to update the method calls accordingly.

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

  2. Press ⌥⌘P or select Refactor | Extract | 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. If you want to declare the generated parameter as a constant, check Declare const in the popup that opens.

  5. Press .

Below you can see the result of the Extract Parameter refactoring:

Before

After

@implementation MyClass { } - (void)print { NSLog(@"%@", self.generateText); } - (NSString *)generateText { return @"Hello, World!"; } @end
@implementation MyClass { } - (void)print { NSLog(@"%@", [self generateText:@"Hello, World!"]); } - (NSString *)generateText:(NSString *)text { return text; }

Extract define

The Extract Define refactoring method defines the selected set of tokens as a macro using the #define directive and replaces the set of tokens with the macro call in the code. The macro definition can be located in the current file or moved to the related header file.

  1. In the editor, select the expression you want to replace with a macro call.

  2. Press ⌥⌘D or select Refactor | Extract | Extract Define 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. If more than one occurrence of the selected expression is found, AppCode will suggest replacing all the occurrences or just the selected one. Select the desired option from the list that opens.

  5. Select a name for the macro from the list of suggestions that opens or type a new one.

  6. Select Put to header if you want to move the macro definition to the header file.

Below you can see the result of the Extract Define refactoring:

Before

After

@interface MyClass () + (CGFloat)ScreenWidth { return [UIScreen mainScreen].bounds.size.width; } @end
#define SCREEN_WIDTH UIScreen @interface MyClass () + (CGFloat)ScreenWidth { return [SCREEN_WIDTH mainScreen].bounds.size.width; } @end

Extract typedef

The Extract Typedef refactoring changes the selected declaration of a type to a typedef definition. You can apply Extract Typedef when actual declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another, or just to make code presentation more clear and readable.

To perform Extract typedef in-place:

  1. In the editor, select the type declaration to be re-declared.

  2. Press ⌥⌘K or select Refactor | Extract | Extract Typedef 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. If more than one occurrence of the selected expression is found, AppCode will suggest replacing all the occurrences or just the selected one. Select the desired option from the list that opens.

  5. Select a name for the typedef from the list of suggestions that opens or type a new one.

  6. Select Put to header if you want to move the typedef definition to the header file.

Below you can see the result of the Extract Typedef refactoring:

Before

After

@interface MyClass () char char1 = '@'; @end
typedef char Symbol; @interface MyClass () Symbol char1 = '@'; @end

Extract method

The Extract Method refactoring lets you take a code fragment that can be grouped together, move it into a separated method and replace the old code with a call to the method.

  1. Select a code fragment you want to extract to a method.

  2. Press ⌥⌘M or from the main menu, select Refactor | Extract | Method.

  3. In the dialog that opens, configure method options, such as visibility, parameter names, selector parts (for Objective-C), and so on. You can also change a name of the method if you need.

    Extract method dialog
    Click OK.

Below you can see the result of the Extract Method refactoring:

Before

After

- (void)method { int a = 1; int b = 2; int c = a + b; }
- (void)method { int a = 1; int b = 2; int c = [self sumA:a sumB:b]; } - (int)sumA:(int)a sumB:(int)b { return a + b; }

Before

After

func method(){ var a = 1 var b = 2 var c = a+b }
func method() { var a = 1 var b = 2 var c = sum(a: a, b: b) } private func sum(a: Int, b: Int) -> Int { return a + b }

Extract block parameter

With the Extract Block Parameter refactoring, you can create a block declaration based on the selected chunk of code within a method. As a result, the created block is passed as a parameter to this method, and all existing usages of this method are changed accordingly.

  1. In the editor, select a chuck of code (within a method) which you want to replace with a block declaration.

  2. Select Refactor | Extract | Extract Block Parameter from the main or context menu.

  3. In the dialog that opens, type the name of the block. If necessary, add/delete parameters, edit parameter names and types, and click Extract:

    Extract block parameter

Below you can see the result of the Extract Block Parameter refactoring:

Before

After

- (int)getAge:(int)birthYear { int age = currentYear - birthYear; // apply refactoring to this line: NSLog([NSString stringWithFormat: @"%d", age]); return age; } -(void)showNameAndAge{ NSLog(self.name); // this usage of the getAge method will be changed // after refactoring [self getAge:1990]; }
- (int)getAge:(int)birthYear withBlock:(void (^)(int))printAge { int age = currentYear - birthYear; // block call printAge(age); return age; } -(void)showNameAndAge{ NSLog(self.name); // new method call [self getAge:1990 withBlock:^(int age) { NSLog([NSString stringWithFormat:@"%d", age]); }]; }

Extract superclass

The Extract Superclass refactoring enables extracting certain members of a class into a newly created superclass.

  1. Select the desired class in the editor.

  2. Select Refactor | Extract | Superclass from the main or context menu.

  3. In the dialog that opens, type the name of the new superclass, select the members that you want to move to the superclass, and click Extract:

    Extract superclass

Below you can see the result of the Extract Superclass refactoring:

Before

After

@interface SubСlass : NSObject { // This variable will be extracted // to a superclass int v; } - (void)initV; @end @implementation SubСlass - (void)initV { v = 20; } @end
// new superclass @interface SuperClass : NSObject { int v; } @end @implementation SuperClass { } @end // changed subclass @interface SubСlass : SuperClass - (void)initV; @end @implementation SubСlass - (void)initV { v = 20; } @end

Extract subclass

The Extract Subclass refactoring enables extracting certain members of a class into a newly created subclass.

  1. Select the desired class it in the editor.

  2. Select Refactor | Extract | Subclass from the main or context menu.

  3. In the dialog that opens, type the name of the new subclass, select the members that you want to move there, and click Extract:

    Extract subclass

Below you can see the result of the Extract Subclass refactoring:

Before

After

@interface SuperСlass : NSObject { // the variable and method // will be moved to the subclass int v; } - (void)initV; @end @implementation SuperСlass - (void)initV { v = 20; } @end
// new subclass @interface SubClass : SuperСlass { int v; } @implementation SubClass { } - (void)initV { v = 20; } @end // changed superclass @interface SuperСlass : NSObject @end @implementation SuperСlass @end

Extract protocol

With the Extract Protocol refactoring you can create a new protocol based on the members of the current class.

  1. Select the desired class in the editor.

  2. Select Refactor | Extract | Protocol from the main or context menu.

  3. In the dialog that opens, type the name of the new protocol, select the members that you want to move there, and click Extract:

    Extract protocol

Below you can see the result of the Extract Protocol refactoring:

Before

After

@interface SuperСlass : NSObject { // the members will be moved // to the protocol int v; } - (void)initV; @end @implementation SuperСlass - (void)initV { v = 20; } @end
//new protocol @protocol NewProtocol <NSObject> - (void)initV; @end //changed class @interface Сlass : NSObject <NewProtocol> { int v; } @end

Extract category

With the Extract Category refactoring you can create a new category based on the members of the current class.

  1. Select the desired class in the editor.

  2. Select Refactor | Extract | Category from the main or context menu.

  3. In the dialog that opens, type the name of the new category, select the members that you want to move there, and click Extract:

    Extract category

Below you can see the result of the Extract Protocol refactoring:

Before

After

@interface Сlass : NSObject { } // the property will be moved to // the category @property(nonatomic, strong) NSString *string; @property(nonatomic) int *number; - (void)initNumber; @end @implementation Сlass - (void)initNumber { self.number = 1; } @end
//new category @interface Сlass (NewCategory) @property(nonatomic, strong) NSString *string; @end @implementation Сlass (NewCategory) @end //changed class @interface Сlass : NSObject { } @property(nonatomic) int *number; - (void)initNumber; @end @implementation Сlass - (void)initNumber { self.number = 1; } @end
Last modified: 25 July 2019