On first use, Xcode’s Find In Project… feature may appear basic. After all it fits on this little rectangle space inside Xcode’s UI.
But beneath that apparently simply interface there’re a ton of features besides a simple find: regular expression searches, find & replace, symbol search, caller search… Another one of those features is search scopes. I’ve been using this more and more lately and that’s what this post is all about.
Search scopes allow you to restrict what files Xcode searches on and thus reduces the time spent searching and the number of results you get back. With larger projects this can be very useful.
By default Xcode will search your entire project but you can change this by clicking on the bottom left button of the Find UI.
This reveals any custom search scopes you have defined and your project’s folder/group layout. Clicking on a custom search scopes or project group restricts the search to those files.
It’s fairly easy to understand how restricting the search to a project group works. So on this post I want to focus on what you can do with custom search scopes.
Lets look at how I’m using this.
Your project will usually be comprised of many different types of files: artwork, strings, storyboards and xibs, etc. Xcode will also search most of these files by default but more often than not you’re just interested in your code.
For example, Secrets is translated in 11 different languages and it has over 40 localized files, that’s over 400 files just for translations. If I search for something included in the localization key my search results explode. It also includes a file containing dictionary words that are used when checking for a password’s strength. This file would appear also appear on many of my searches.
In my case, my source code comes down to Objective-C, C, and Swift files. So I just create a code that search for any file with the .m, .h, .c and .swift extensions.
As I’ve mentioned, Secrets is translated in 11 different languages and has over 400 files related to localization.
But most of the time when searching for localized strings I’m just interested in the Base localization. I never touch the translated files inside Xcode, these are synced with my translation service. So by using this scope, whenever I’m searching for a localized string I just get back the base localization files.
And since the base localization files are always inside the Base.lproj folder, the search scope is very easy to define as seen above. Also note I’ve included localized Storyboards and XIBs on this scope since they’ll also contain your base strings1.
Last but not least, when your working on a given feature it’s usual to have a set of modified files you’re working on. Sometimes you’ll want to search for something on only this set of related files.
Simply using the source control status does the trick.
One more thing
Finally, you can select more than one scope at a time by command+clicking on all the scopes you want. So if for example I were to search for some code on the iOS version of Secrets I could click on the “Source Code” scope and then command+click on the “Secrets Touch” project folder.
For more Xcode tips & tricks click here.
- If you’re not familiar with the predicate editor, you need to option+click the plus sign to create a subexpression. [return]