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
Position the caret at the name of the method/function that you want to refactor.
Press ⌃ F6. Alternatively, select Change Signature dialog opens.
from the main or context menu. TheIn 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 and specify the new parameter's properties in the corresponding table row.
To remove a parameter, select any row and click .
To reorder the parameters, click and .
To see the expected changes and make adjustments prior to the refactoring, click Preview (available for Objective-C only).
Click Refactor.
Examples
Swift: Change the method signature
Let's refactor the following method:
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.
Place the caret at the method's name and press ⌃ F6. The Change Signature dialog opens:
In the Name field, type the new name:
setUpText
.Swap the parameters by clicking or :
Rename the parameters and add internal names for them to make the method declaration more fluent and precise: change
cell
anditem
tofor cell
andwith item
. To do this, select the parameter in the table and type the new values in the fields that appear:In the Signature Preview field, you can see how the new method signature will look:
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
Place the caret at the
isPasswordValid
function and press ⌃ F6.In the dialog that opens, select Block in the Callable Type list.
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)
// ...
}
|