AppCode 2019.2 Help

Convert

Convert refactorings allow converting methods to functions or code blocks and vice versa, as well as converting properties to instance variables.

These refactorings are invoked from the Refactor menu and available for Objective-C only. You can also use the Change signature refactoring for the same purposes.

Convert to method

The Convert to Method refactoring lets you convert a function or a block of code to a method.

  1. In the editor, place the caret within the name of the method or select a block of code that you want to convert to a method.

  2. From the main or context menu, select Refactor | Convert to Method.

  3. In the dialog that opens, make changes if necessary:

    Convert Module Groups to method

  4. To perform the refactoring, click Refactor. To see the expected changes and make the necessary adjustments prior to actually performing the refactoring, click Preview.

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

Before

After

// function to convert to method NSString *generateText() { return @"Hello, World!"; } -(void)print{ // function usage NSLog(generateText()); }
// new method + (NSString *)generateText { return @"Hello, World!"; } -(void)print{ // method usage NSLog([MyClass generateText]); }

Convert to function

The Convert to Function refactoring lets you convert a method or a block of code to a function.

  1. In the editor, place the caret at the name of the method or select a block of code that you want to convert to a function.

  2. From the main or conext menu, select Refactor | Convert to Function.

  3. In the dialog that opens, make changes if necessary:

    Convert to function

  4. To perform the refactoring, click Refactor. To see the expected changes and make the necessary adjustments prior to actually performing the refactoring, click Preview.

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

Before

After

// method to convert to a function -(NSString *)generateText { return @"Hello, World!"; } -(void)print{ // method usage NSLog(self.generateText); }
// new function NSString *generateText() { return @"Hello, World!"; } -(void)print{ // function usage NSLog(generateText()); }

Convert to block

The Convert to Block refactoring lets you convert a function or a method to a block of code.

  1. In the editor, place the caret at the name of the method/function that you want to convert to a block.

  2. From the main or context menu, select Refactor | Convert to Block.

  3. In the dialog that opens, make changes if necessary:

    Convert to block

  4. To perform the refactoring, click Refactor. To see the expected changes and make the necessary adjustments prior to actually performing the refactoring, click Preview.

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

Before

After

// this method will be // converted to block - (int)sumA:(int)a sumB:(int)b { return a+b; } - (void)showResult{ // method usage NSLog([NSString stringWithFormat:@"%d", [self sumA:10 sumB:20]]); }
- (void)showResult{ NSLog([NSString stringWithFormat:@"%d", ^int(int a, int b) { return a + b; }(10, 20)]); }

Convert to property

The Convert to Property refactoring lets you convert an instance variable to a property.

  1. Place the caret within the editor window.

  2. From the main or context menu, select Refactor | Convert to Property.

  3. In the dialog that opens, select the instance variable(s) you wish to convert:

    Convert Module Groups to property

  4. Click OK to perform the refactoring.

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

Before

After

@interface Person : NSObject @end @implementation Person { @private // instance variable to convert // to property NSString *_name; } @end
@interface Person : NSObject // new property @property(nonatomic, strong) NSString *name; @end @implementation Person { } @end

Convert to instance variable

The Convert to Instance Variable refactoring lets you convert a property to an instance variable.

  1. Place the caret within the editor window.

  2. From the main context menu, select Refactor | Convert to Instance Variable.

  3. In the dialog that opens, select the properties you wish to convert:

    Convert Module Groups to instance variables

  4. Click OK to perform the refactoring.

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

Before

After

@interface Person : NSObject // property to convert @property(nonatomic, strong) NSString *name; @end @implementation Person { } @end
@interface Person : NSObject @end @implementation Person { @private // new instance variable NSString *_name; } @end
Last modified: 25 July 2019