Xcode Tip: Using Search Results as To-Do List

Xcode’s search functionality has some powerful tools, but not all of them are obvious at first sight. In this article we have a look at how to use the Xcode search results as a to-do list, and also how we can integrate it into our refactoring workflow.

Starting a Refactoring

Let’s say we have to change a method, that is used in multiple places of our code base. The first choice is the built-in refactoring options that we can access from the Editor menu Editor > Refactor > … or by selecting the method with a double-click and a right-click on it to access the context menu.





Though sometimes the refactoring options appear disabled, this can happen especially in big projects, when Xcode is not able to identify where the code is being used. So let’s use the search function to find all the code places that we need to convert.

Creating a To-Do List

First we open the Find Navigator in the left Xcode panel by using the keyboard shortcut command + 4. Here we can enter the name of our method to get a list with all occurrences of it in our project. Sometimes our method name is mentioned in comments and other places that we don’t want to change. Now let’s use one of the more hidden features of Xcode to remove the search result entries where we don’t want to refactor our code. So if we select an entry in our search results, we can hit the delete key to remove it from the search results. 





Once we removed all the search entries that we don’t need to refactor, we got our to-do list ready and can start our code changes. Now we can simply go through our search result list from top to bottom, always selecting the first entry, doing our code changes, and hitting the delete key to remove the entry from the to-do list.

Conclusion

Even though Xcode doesn’t offer the best refactoring features for Swift code, we can use its powerful search features, and incorporate them in our refactoring workflow. By using the search results as a to-do list we can make sure that we don’t miss a piece of our code base in a refactoring. 


Thanks for reading! Enjoy coding!



Created with Xcode 12.4.
Xcode is a trademark of Apple Inc., registered in the U.S. and other countries.