AppCode 2023.1 Help

Convert

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

These refactorings are invoked from the Refactor menu. 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 block into a method.

  1. In the editor, place the caret at the function or block that you want to convert into 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 expected changes and make necessary adjustments prior to performing the refactoring, click Preview.

Before

After

// This function will be converted into a method BOOL isPasswordValid(NSString *password) { return password.length > 4; } - (void)performLogin { // ... // Function's usage if (isPasswordValid(password) == NO) { // ... } }
// New method + (BOOL)isPasswordValid:(NSString *)password { return password.length > 4; } - (void)performLogin { // ... // Method's usage if ([LoginViewController isPasswordValid:password] == NO) { // ... } }

Convert to function

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

  1. In the editor, place the caret at the method or block that you want to convert into a function.

  2. From the main or context 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 performing the refactoring, click Preview.

Before

After

// This method will be converted into a function - (void)setIsValid:(BOOL)isValid forField:(UITextField *)field { field.textColor = isValid ? [UIColor blackColor] : [UIColor redColor]; } - (void)performLogin { // ... if (isEmailValid == NO) { // Method's call [self setIsValid:NO forField:self.textFieldEmail]; } }
// New function void setIsValid(BOOL isValid, UITextField *field) { field.textColor = isValid ? [UIColor blackColor] : [UIColor redColor]; } - (void)performLogin { // ... if (isEmailValid == NO) { // Functions's call setIsValid(NO, self.textFieldPassword); } }

Convert to block

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

  1. In the editor, place the caret at the method or function that you want to convert into 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 performing the refactoring, click Preview.

Before

After

// This function will be converted into a block BOOL isPasswordValid(NSString *password) { return password.length > 4; } - (void)performLogin { // ... // Function's call if (isPasswordValid(password) == NO) { // ... } }
- (void)performLogin { // ... // New block if (^BOOL(NSString *password) { return password.length > 4; }(password) == NO) // ... }

Convert to property

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

  1. Place the caret at the instance variable that you want to convert into a property.

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

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

    Convert instance variable to property
  4. Click OK to perform the refactoring.

Before

After

@interface AlertParams : NSObject { // These instance variables // will be converted to properties @public NSString *_title; NSString *_message; } @end
@interface AlertParams : NSObject // New properties @property (nonatomic, strong) NSString *title; @property (nonatomic, strong) NSString *message; @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 or context menu, select Refactor | Convert to Instance Variable.

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

    Convert properties to instance variables
  4. Click OK to perform the refactoring.

Before

After

@interface AlertParams : NSObject // These properties will be converted // to instance variables @property (nonatomic, strong) NSString *title; @property (nonatomic, strong) NSString *message; @end
@interface AlertParams : NSObject { // New instance variables @public NSString *_title; NSString *_message; } @end
Last modified: 13 December 2022