AppCode 2021.2 Help

Change signature

The Change Signature refactoring combines several different modifications that can be applied to a method/function signature. You can use this refactoring to:

  • change the method/function name and return type

  • add, remove, and reorder parameters

  • change parameter names and types

  • change the visibility type (for Swift) and the callable type (for Objective-C)

When changing a method/function signature, AppCode searches for all usages of the method/function and updates all the calls, implementations, and override replacements of the method/function that can be safely modified to reflect the change.

Changing a method/function signature

  1. Position the caret at the name of the method/function that you want to refactor.

  2. Press ⌃ F6. Alternatively, select Refactor | Change Signature from the main or context menu. The Change Signature dialog opens.

    Change signature refactoring
  3. In the Change Signature dialog, make necessary changes to the method/function signature depending on your needs:

    • For Objective-C: change the type of a callable procedure. In the Callable Type filed, select Function, Method, or Block.

    • For Swift: change the visibility type. In the Visibility field, select public, internal, private, or another type.

    • Change the method/function name. To change the name, edit the text in the Name field.

    • Change the method/function return type by editing the contents of the Return type field.

    • Manage the method/function parameters. To configure the parameters, use the table and buttons in the Parameters area:

      • To add a new parameter, click The Add button and specify the new parameter's properties in the corresponding table row.

        Add parameters

      • To remove a parameter, select any row and click The Remove button.

      • To reorder the parameters, click The Up icon and The down icon.

  4. To see the expected changes and make adjustments prior to the refactoring, click Preview (available for Objective-C only) .

  5. Click Refactor.

Examples

Swift: Change the method signature

Let's refactor the following method:

func addTextForCell(item: MyListItem, cell: UITableViewCell) { let label = cell.viewWithTag(1000) as! UILabel label.text = item.text }

This method displays the text stored in the text property of the MyListItem class in a table view cell (UITableViewCell). With the Change Signature refactoring, you can make the method declaration more descriptive.

  1. Place the caret at the method's name and press ⌃ F6. The Change Signature dialog opens:

    Change Signature
  2. In the Name field, type the new name: setUpText.

  3. Swap the parameters by clicking the Move down button or the Move up button:

    Swap the method parameters
  4. Rename the parameters and add internal names for them to make the method declaration more fluent and precise: change cell and item to for cell and with item. To do this, select the parameter in the table and type the new values in the fields that appear:

    Rename parameters
  5. In the Signature Preview field, you can see how the new method signature will look:

    Signature preview

    Click Refactor to apply the changes. The method signature will be changed in the method declaration and all its usages:

    func setUpText(for cell: UITableViewCell, with item: ChecklistItem) { let label = cell.viewWithTag(1000) as! UILabel label.text = item.text }

Objective-C: Convert a function into a block

  1. Place the caret at the isPasswordValid function and press ⌃ F6.

  2. In the dialog that opens, select Block in the Callable Type list.

  3. Click Refactor.

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) // ... }
Last modified: 27 August 2021