onlinecode

Xcode refactoring options explained with examples – onlinecode

Xcode refactoring options explained with examples – onlinecode

In this post we will give you information about Xcode refactoring options explained with examples – onlinecode. Hear we will give you detail about Xcode refactoring options explained with examples – onlinecodeAnd how to use it also give you demo for it if it is necessary.

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.

Architecting SwiftUI apps with MVC and MVVMAlthough you can create an app simply by throwing some code together, without best practices and a robust architecture, you’ll soon end up with unmanageable spaghetti code. Learn how to create solid and maintainable apps with fewer bugs using this free guide.

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

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

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

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.

https://www.onlinecode.org/wp-content/uploads/2019/07/multi-cursor.mp4
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.

https://www.onlinecode.org/wp-content/uploads/2019/07/extract-method-1.mp4
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.

https://www.onlinecode.org/wp-content/uploads/2019/07/extract-occurrences.mp4
Extract occurrences refactoring method

Refactoring options with a switch in Swift

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.

https://www.onlinecode.org/wp-content/uploads/2019/07/expand-switch-cases-1.mp4
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.

https://www.onlinecode.org/wp-content/uploads/2019/07/Xcode-init-generation-extension.mp4
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.

Architecting SwiftUI apps with MVC and MVVMAlthough you can create an app simply by throwing some code together, without best practices and a robust architecture, you’ll soon end up with unmanageable spaghetti code. Learn how to create solid and maintainable apps with fewer bugs using this free guide.

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!

 

Hope this code and post will helped you for implement Xcode refactoring options explained with examples – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version