Extract function
Last modified: 28 March 2023Language: Swift, Objective-C
Shortcut: Ctrl+Alt+M
The Extract Function refactoring lets you take a code fragment that can be grouped together, move it into a separated function and replace the old code with the function call.
The Extract Function refactoring has the following limitations:
Refactoring does not work with multiple output values in the automatic mode. You have to change your code before applying the refactoring, for example, you may introduce a special data-class that contains all output values.
Refactoring does not work for a code fragment which conditionally returns from the containing method and is not placed at the end of it.

To reverse the Extract Function refactoring, press Ctrl+Alt+N to invoke the Inline refactoring.
Extract a function
Select a code fragment you want to extract to a function.
Press Ctrl+Alt+M or from the main or context menu, select Refactor | Extract/Introduce | Function.
In the dialog that opens, configure function 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.
Click Extract.
Before | After |
---|---|
|
|
Before | After |
---|---|
|
|
Thanks for your feedback!