Give your simulator superpowers

RocketSim: An Essential Developer Tool
as recommended by Apple

Xcode refactoring options explained with examples

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.

Renaming using the Xcode refactoring option
Renaming using the Xcode refactoring option

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.

Xcode renaming overview
Xcode renaming overview

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”

The Xcode Edit all in scope refactoring method
The Xcode Edit all in scope refactoring method

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.

How do you stay current as a Swift developer?

Let me do the hard work and join 19,144 developers that stay up to date using my weekly newsletter:

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.

Xcode multi-cursor editing

This edit mode can be enabled as followed:

  • shift + control + left mouse click creates a new cursor on every click
  • shift + control + arrow up creates a new cursor at the line above
  • shift + control + arrow down creates a new cursor at the line bellow
  • option + drag creates 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.

Extracting methods using Xcode refactoring

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.

Extract occurrences refactoring method

Refactoring options with a switch in Swift

Exhaustive switch error in Xcode
Exhaustive switch error in Xcode

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.

Expand switch cases and add missing cases

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.

Xcode extension for generating initializers

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.

Conclusion

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.

Thanks!

 
Antoine van der Lee

Written by

Antoine van der Lee

iOS Developer since 2010, former Staff iOS Engineer at WeTransfer and currently full-time Indie Developer & Founder at SwiftLee. Writing a new blog post every week related to Swift, iOS and Xcode. Regular speaker and workshop host.

Are you ready to

Turn your side projects into independence?

Learn my proven steps to transform your passion into profit.