Xcode Search: the Hidden Gems

As software developers, we spend a surprisingly large amount of time reading code. Robert C. Martin points this out in Clean Code:

Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code.

Reading through the whole codebase is just not practical. When you join a new project, browse through some open source library or just work on a large codebase, search becomes an immensely useful part of your toolkit. So, let's dive in into some hidden 💎 of Xcode's Find Navigator.

#1: Patterns

Searching for text in Xcode is quite easy. Type Command-Shift-F and off you go. That's not enough, though, when we want to perform a more advanced search. If, say, we wanted to search for strings containing URLs we'd end up with a pretty complicated regular expression. This is so common case, that Apple decided to help us by introducing the ability to search for wildcard patterns.

To do that, we start by clicking on the loupe button with the disclosure indicator next to it:

Search Options

Then, we are presented with the list of the available patterns:

List of Available Patterns

Clicking on a pattern inserts it into the search field. Just like that we got a list of strings representing URLs:

Strings Representing URLs (in Eidolon, thanks for OS Artsy!)

Patterns are a good middle ground between simple text searches and regular expression.

#2: Definitions

Xcode also provides search options leveraging its understanding of the code structure. Let's say, we want to find all singletons that crept into our project. We'll use a heuristic here by assuming that all of them have methods or properties named shared*.

If we simply search for shared we get a lot of results:

Default Search Results

To narrow the results we have to click on Text (which doesn't look like a clickable element!) and choose Definitions instead:

Definitions Option

And we're done!

Search Results Narrowed to Definitions

With Definitions filter we can also search for a class, struct, and enum names; enum cases and more.

#3: Search Scopes

Choosing a project or a group that we want to search in is often enough:

Scopes Menu

Let's imagine that we work in a mixed Objective-C/Swift codebase. (It wasn't that hard to imagine, right? 😉). We want to search for something only in Swift files. We can do that easily thanks to custom Search Scopes. Click on New Scope... button and limit your search results by filtering for: Location, Name, Path, Path Extension and Type. Type seems like a great fit for our search but unfortunately, there isn't Swift on the list:

No Swift here ¯\_(ツ)_/¯

Instead of that, though, we can filter by Path Extension, like this:

Scope Filtering .swift Files

#4: Call Hierarchy

A recent feature added in Xcode 7, is an ability to search call hierarchies. With it, we can get a high-level overview of how our method or function is used. I don't use it often but when I do it tends to save a lot of time.

Call Hierarchy Search Example

As you can see, Find navigator in Xcode is pretty great. Searching isn't always the best solution to find what we're looking for, though. For example, when we have a cursor over some property it's better to click Command-Control-J to jump to its definition than use search. Or when we want to see who calls a method we can check Callers in Related Items (Control-1) menu without even leaving the text view.

Related Items Menu


Search in Xcode gets more and more powerful each year. It helps us find and understand our own and third-party code. As with all other tools (or anything in life), it comes with some tradeoffs. We have to remember to use the best tool for the job.