AppCode 2023.1 Help

Swift refactorings in AppCode

In this tutorial, you will get familiar with the AppCode refactoring tools available for Swift. On a simple project, we will show you how you can quickly and easily improve your code using refactorings. Along the way, you'll also get familiar with some of inspections and intention actions available in AppCode.

Before you start

To be able to repeat the steps of this tutorial, download the iOSConferences project. It is a simple SwiftUI application that loads a list of conferences from a local JSON file. You can look at how we created a similar application in the Create a SwiftUI application in AppCode tutorial. Now we will try to optimize its code and make it more concise and easier to read.

In the main menu, select View | Tool Windows | TODO. This will open the TODO tool window where you can see all the places in the project that we are going to refactor:

The TODO tool window


Renaming is one of the simplest and most frequently used operations that can significantly improve readability of your code. You may need it, for example, when you notice that a class, method, or variable name either does not follow the company's guidelines, or its functionality was extended, and the name should be updated accordingly. The Rename refactoring in AppCode allows you to safely change the name of a file, class, method, or variable throughout the whole project.

In the TODO tool window, double-click the Rename comment to navigate to the ContentView structure. ContentView is a SwiftUI view that displays the list of conferences. Though the application consists of two views only, the name of this one definitely could be more precise. So, let's rename it to ConferencesList:

  1. Place the caret at the structure name and press Shift+F6. Alternatively, select Refactor | Rename from the main menu. When the structure name gets highlighted, type the new name:

    The Rename refactoring
  2. The gray the Comments icon icon next to the highlighted symbol means that the refactoring won't be performed in comments and string literals. Click this icon or press Tab and in the popup that appears, select the Comments and strings checkbox:

    the Rename popup
  3. Press Enter to perform the refactoring. This will rename all occurrences of the symbol in source code, comments, and string literals. Moreover, the ContentsView.swift file will also be renamed.

  4. Now let's make sure that AppCode has changed the structure name everywhere. Press Ctrl+Shift+F. In the dialog that opens, enable case sensitive search by clicking App actions match case hovered and type the old name — ContentView. The only ContentView occurrence found is the ContentView_Previews class. It was not renamed by the refactoring since it is a different symbol. Still, for the sake of consistency, rename it to ConferencesList_Preview. You can do it directly in the preview of the Find in File dialog:

    Edit code in preview

    To close the dialog, just click somewhere outside.

  5. Delete the //TODO: Rename comment. To delete the whole line at once, place the caret at it and press Ctrl+Y.

Introduce a variable

The Introduce Variable refactoring may come in handy when you want to make your code more readable and clear, lighten up complex constructions, or avoid code repetition.

In the TODO tool window (View | Tool Windows | TODO), double-click the Introduce Variable comment. This will bring you to the List object with the loadFile() method passed as its parameter. You can extract the expression in brackets to a variable named conferencesData. This will make the code more clear and allow you to get rid of the type cast. To do this:

  1. Place the caret at the loadFile() method call and press Ctrl+Alt+V. Alternatively, press Ctrl+Alt+Shift+T and then 5 to select the refactoring from the list.

  2. Select the expression in brackets and press Enter:

    Select an expression to extract

    The variable declaration will appear right above the List object.

  3. Now you can choose if you want to declare the new variable with var and specify the variable type explicitly:

    Variable options

    Leave the both options unchecked.

  4. The variable name suggested by default is conferences. Rename it to conferencesData:

    Rename the variable

    Press Enter.

  5. With the caret placed at the variable declaration, press Alt+Shift+Up to put this line before the body structure and press Ctrl+Alt+L to fix the code formatting:

    Move the line and reformat code
  6. Let's specify the variable type in the declaration and remove the type cast in the variable value. Press Alt+Enter to invoke available intention actions and select Add explicit type:

    Intention action

    Remove as [Conference].

  7. Look at the top right corner of the editor — the inspection widget has appeared there. This mean that AppCode detected some code that can be improved. Click the widget to open the Problems tool window:

    The weak warning

    You see there is a redundant return keyword at line 16. You can fix it by pressing Alt+Enter (either in the editor or in the Problems tool window) and selecting Remove redundant 'return'.

  8. Remove the TODO comment (Ctrl+Y).

Change signature

The Change Signature refactoring allows you to change a set of method parameters and their types, the return and throw types, and method visibility. The refactoring is performed in all method calls, implementations, and overrides.

In the TODO tool window (View | Tool Windows | TODO), double-click the Change Signature comment. This will open the loadFile() method in Data.swift. This method loads data from the local conferencesList.json file. The file name is specified in the method body, however, it would make more sense to pass it as a parameter. Let's use the Change Signature refactoring to do this:

  1. Place the caret at the loadFile() method name and press Ctrl+F6. Alternatively, press Ctrl+Alt+Shift+T and then 2 to select the Change Signature refactoring from the list.

  2. In the Change Signature dialog that opens, click the Add icon to add a new parameter and enter values in the following fields:

    • Internal Name: filename

    • Type: String

    • Default value: "conferencesData.json"

    This will add an internal parameter of String type that you can see in the preview below:

    The Change Signature dialog
  3. Click the Preview button to see all the usages of the method that will be affected by the refactoring. In our case, there are two places — the method declaration and call:

    The Change Signature preview
  4. Click Do Refactor.

  5. Now replace all occurrences of "conferencesData.json" with the filename parameter within the method body. To do this, select one of the occurrences and press Ctrl+Alt+Shift+J — the carets will be set at all the occurrences of the selected string, and you can type filename instead:

    Replace occurrences
  6. Navigate to the method call to make sure the refactoring has been applied correctly. To do this, place the caret at the method name and press Ctrl+B. The loadFile() method is called with the "conferencesData.json" string passed as its parameter:

    Changed signature in the method call
  7. Go back to the method declaration Ctrl+B and remove the TODO comment.

Extract method

With the Extract Method refactoring, you can move a code fragment to a separate method and, therefore, make your code more readable, avoid duplications, and isolate independent parts of code if needed.

From the TODO tool window (View | Tool Windows | TODO), select the last comment — Extract Method. Here you see that the end and start dates are displayed in the Text element.

If you run the application Shift+F10 and select the dotSwift conference, the program will crash at this line. The point is that the value of the end property in this case equals nil, and the application will crash trying to unwrap the optional. Let's extract the expression in the brackets into a separate method and add some code that will fix the bug:

  1. Place the caret at the expression in the brackets and press Ctrl+Alt+M. Alternatively, press Ctrl+Alt+Shift+T and then 6.

  2. In the popup that opens, select the expression that should be extracted and then press Enter:

    Select an expression to extract
  3. In the Extract Method dialog, type the method name — textDates — and click Refactor:

    The Extract Method dialog

    The new method appears in the ConferenceDetails structure, and its call is added in place of the extracted expression:

    The extracted method
  4. Extract Ctrl+Alt+V conference.start.dateToString() to a separate variable named result. You can use the Introduce Variable refactoring Ctrl+Alt+V to do it:

    private func textDates() -> String { var result = conference.start.dateToString() return "\(result) - \((conference.end?.dateToString())!)" }
  5. In the if let block, add the code for displaying dates with non-nil end values:

    private func textDates() -> String { var result = conference.start.dateToString() if let end = conference.end { result = "\(result) - \(end.dateToString())" } return result }
  6. Move the newly created method from ConferenceDetails to the Conference class and delete all the usages of the conference variable:

    private func textDates() -> String { var result = start.dateToString() if let end = end { result = "\(result) - \(end.dateToString())" } return result }

    Go back to the ConferenceDetails view and replace textDates() with conference.textDates(). The textDates() method call gets highlighted. Hover the cursor overt it to see the error message:

    The error message

    Apply a quick-fix to make the method internal: press Alt+Enter and select the corresponding action from the list:

    Make the method internal
  7. Remove the TODO comment.

What's next

In this tutorial, we covered the popular Swift refactorings and showed how you can use them in your project. You can find the description of all available refactorings for Swift and Objective-C in the Refactorings section. Also, refer to the Code inspections and Intention actions sections to learn how code inspections and intention actions in AppCode can help you improve your code.

Last modified: 21 November 2022