A brand new Xcode refactoring engine was introduced in Xcode 9. Although this is quite a few Xcode versions ago it is still a quite unknown functionality for a lot of us. You might have read my blog post on command-click options but do you know how to extract methods or variables?
The Xcode refactoring engine can both transform code locally withing a single Swift source file and globally over multiple files, like renaming a method that occurs in multiple files and even different languages.
There’s a lot to cover, so let’s go over them!
Renaming Swift code
Renaming Swift code is probably something we’ve all done either manually or automatically. Xcode offers several refactoring options to make this sometimes large code change a piece of cake.
The renaming refactoring option can be selected from the refactoring menu by right-clicking the piece of Swift code you want to rename. It will open a new edit overview which will show all the references which will be renamed across multiple files.
After filling in a new name you can apply the renaming by pressing the blue rename button at the top right corner.
Renaming using “Edit all in scope”
Renaming code within a single file can be done by using the “Edit all in scope” refactoring option. CMD + Click on a variable, method or class name and you can start renaming.
Xcode multi-cursor editing to edit multiple lines simultaneously
A great but hidden feature is the Xcode multi-cursor editing option to edit multiple lines simultaneously. It allows you to, for example, rename multiple parameters at the same time.
This edit mode can be enabled as followed:
left mouse clickcreates a new cursor on every click
arrow upcreates a new cursor at the line above
arrow downcreates a new cursor at the line bellow
dragcreates a new cursor on every line you drag
This option can make it really easy to rename a lot of code at once but does not rename all references. Therefore, this method requires you to manually rename all references afterward.
Extracting methods using Xcode refactoring
A great way to refactor your code and make it more readable is by extracting pieces of code into its own method. It’s as easy as selecting the code and clicking the “Refactor → Extract to Method” option.
Extract Variables and Extract All Occurrences
If you have multiple expressions close to each other you might want to extract it to its own variable. You can do this for a single expression with the “Extract to Variable” method and for multiple occurrences with the “Extract All Occurrences” option.
Refactoring options with a switch in Swift
While working with enums we use the switch a lot in Swift. Often times, the Swift compiler will help us to refactor our code and add missing cases with the common “Exhaustive” error alert.
However, sometimes you have defined your switch with a default case which will prevent Xcode from showing the error. In this case, you can make use of the “Expand Switch Cases” option. However, best practice would be to make use of @unknown default and let Xcode warn you.
Using Xcode extensions
Although the Xcode refactoring options are great, it might not be enough. Therefore, it’s worth to explore other options which can be enabled with Xcode extensions.
A great list of available extensions can be found on Github. Take your time, explore, and let me know on Twitter which ones you use!
Note: There’s a built-in solution which works for classes already. Click on the class name → Refactor → Generate Memberwise Initializer. Thanks to Vadim Bulavin for pointing this out!
Adding your own refactoring methods
The Xcode refactoring engine is open-sourced in the swift repository and allows you to write your own refactoring methods. An intensive guide including potential refactoring ideas can be found at the official Swift blog.
The Xcode refactoring engine offers a lot of great ways to refactor your code. I didn’t even cover all refactoring options but most of the missing ones are shown in compile errors either way.
Let me know which ones you use on Twitter and feel free to give feedback on which refactoring methods should be listed here.