Easy way to re-format/beautify code in Xcode

Learn how to enhance your code's readability using Xcode's Re-Indent dan XCFormat feature. Follow step-by-step instructions for a cleaner code structure.

Easy way to reformat/beautify code in Xcode
This article contains tips and tricks for formatting code written in Xcode. In addition to using Xcode's built-in features, these tips will also use Xcode extensions.

One of the first things I looked for when I started using Xcode was a code formatter, which is usually available in every editor or IDE. However, I couldn't find it. All that was available was a feature to tidy up the code's indent positions, which of course is not very optimal. Here are 2 ways to tidy up or format code in Xcode.

Re-Indent: A Built-In Solution

This is a built-in Xcode feature which, as the name implies, mainly functions to tidy up the indents of code blocks that have been written. Although not very comprehensive, this Re-Indent feature can be said to be quite helpful.

Using this Re-Indent feature is very easy. Please open the file you want to tidy up. Then select the lines you want to tidy up. In the example below, the Author wants to tidy up the code starting from line 34 to 43.

Easy way to reformat/beautify code in Xcode
Tidying up code with Re-Indent

After selecting the lines you want to tidy up, click the Editor > Structure > Re-Indent menu.

Easy way to reformat/beautify code in Xcode
Re-Indent Menu

The selected lines will automatically be tidied up. The results are quite good and suitable for certain conditions.

Easy way to reformat/beautify code in Xcode
Re-Indent Results

Conclusion:
The Re-Indent feature will only fix the indentation. This feature will not fix the number of spaces or bracket positions that are poorly positioned. So, this feature is quite helpful, but not optimal.

XCFormat Extension: Taking Code Formatting to the Next Level

Another alternative is to use Xcode extensions, which of course requires you to first install the extensions. If you are looking for a code formatter extension for Xcode, you may find several alternatives. However, in the Author's experience, one of the most suitable extensions that is always up-to-date is XCFormat.

#Installing XCFormat

The first step is to install XCFormat from the App Store. Therefore, please open the App Store, then search for XCFormat, or please click on this App Store link.

Easy way to reformat/beautify code in Xcode
XCFormat in the App Store

#Activating the XCFormat Extension

After you have finished installing XCFormat. Next you need to activate the XCFormat extension. Please run XCFormat.

Easy way to reformat/beautify code in Xcode

Information from XCFormat will appear along with steps to activate XCFormat.

Easy way to reformat/beautify code in Xcode
XCFormat configuration

Next, please open your Mac's System Preferences. It can be opened by clicking the Apple button in the top left corner. Then click the System Preferences menu.

Easy way to reformat/beautify code in Xcode
System Preferences

In the System Preferences options, please search for and click on the Extensions menu.

Easy way to reformat/beautify code in Xcode
Menu Extensions

In that System Preferences window, please click on the Xcode Source Editor section on the left. Then please check the XCFormat section. This will successfully activate the XCFormat extension.

Easy way to reformat/beautify code in Xcode
Activate XCFormat

#Formatting Code with XCFormat

If Xcode is currently running when you activate XCFormat, please close Xcode first, then run Xcode again.

Please open the file you want to reformat. Then select the lines you want to format. For the example below it is lines 22 to 32.

Easy way to reformat/beautify code in Xcode
Before Reformatting with XCFormat

Then click the Editor > XCFormat > Format Active File or Format Selected Lines menu.

Easy way to reformat/beautify code in Xcode
XCFormat > Format Active File

The Format Active File menu is to format the entire file you have open. While the Format Selected Lines menu is to format the file you have open only on the lines you have selected.

The selected code lines or entire file will be formatted very neatly. Not only the indentation, but also the position of the brackets and spaces will also be fixed.

Easy way to reformat/beautify code in Xcode
After Reformatting with XCFormat

#Setting Shortcuts (Optional)

If it is inconvenient to always have to click the Editor menu, etc. every time you want to re-format a file, then you can set shortcuts for those menus as follow.

Click Xcode > Preferences.

Easy way to reformat/beautify code in Xcode
Xcode preferences

On the Preferences window, click the Key Bindings tab.

Easy way to reformat/beautify code in Xcode
Key Bindings/Shortcut Feature

In the filter field, please type xcformat to display the menu options for XCFormat.

Easy way to reformat/beautify code in Xcode
Shortcut for XCFormat

Then click the Key column, then press the key combination you want. For this example, the Author sets the Ctrl + Shift + Command + I shortcut for the Format Active File menu.

Easy way to reformat/beautify code in Xcode
Setting a Shortcut for XCFormat

By setting this keyboard shortcut, you will be able to easily format code just by pressing a combination of keyboard buttons.

Conclusion:
By using the XCFormat extension, the formatting results are clearly much better when compared to using Re-Indent. Because what is formatted is not only the indentation, but also other code components, such as spaces, bracket positions and others. In addition, XCFormat also supports code written in Swift and Objective-C.

That concludes this tip and trick. Please try it out, hopefully this little bit will be useful.

Links:
App Store
XCFormat