Write code faster, build better tests, and navigate around projects more efficiently.
Xcode is a huge program, and if you can learn one or two new tips that help you work more effectively you can save hours of time every week.
In this article I want to walk you through 24 quick Xcode tips that help you write code faster, build better tests, and navigate around large projects more efficiently – there’s something for everyone!
SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Swift can synthesize memberwise initializers for structs, but not for classes. Fortunately, Xcode can generate the code for us: select your class name, then go to the Editor menu and choose Refactor > Generate Memberwise Initializer. Done!
When building software, it’s important to make sure your app works for everyone. One great way to do that is with Xcode’s environment overrides panel, which lets you adjust Dynamic Type size, accessibility options, and appearance all in one place.
You probably already know that you can click after any open { or close } brace to have Xcode highlight its matching brace, but another useful tip is that you can double click a brace to select the entire block of code it contains.
Xcode can check if your spelling is correct, and it even understands that camelCaseNames should be treated as separate words. Go to the Edit menu and choose Format > Spelling and Grammar > Check Spelling While Typing, and Xcode will check your code for typos.
If you have a Swift file with many errors, you can use Xcode’s fix-its to try to correct them one by one. But a faster way is to go to the Editor menu and choose Fix All Issues to apply fix-its all at once. (Make sure and double-check which changes were made!)
When you search using Xcode’s find navigator, you can click to view individual results and make any changes you want. But when you’re done, I recommend you hit Backspace to remove the search item from the results, so you can see which results you have left to check.
This is the simplest tip I know, but it will save you time if you don’t already know it. Whenever you see Automatic Preview Updating Paused in your SwiftUI canvas, press Opt+Cmd+P to resume previewing. Alternatively, use Opt+Cmd+Return to hide the canvas completely.
If you frequently move from Xcode to the simulator, tile them side by side. With the simulator active, go to the Window menu and choose Tile Window To Right Of Screen, then select Xcode on the left. You can adjust the split so the simulator sits snugly on the right.
Xcode has great code completion built right in, but sometimes as you scroll through the options you might find the names are too long to fit. Fortunately, you can just grab the edge of the autocomplete popup and drag it as wide as you want!
Breakpoints are great for debugging – place them by selecting a line number, or remove them by right-clicking and choosing Delete Breakpoint. You can also remove one by dragging it away, but a fast method is to use Cmd+\ to toggle a breakpoint on the current line.
Sometimes the output from one test affects the input for another. Xcode has a solution: go to the Product menu, hold down Option, then click Test. Inside the Info tab, click Options then check Randomize Execution Order to run tests in a different order every time.
Xcode’s jump bar (Ctrl-6) is the best way to jump around long files quickly, letting you browse all properties and methods in your file. For longer files, start typing with the jump bar open and Xcode will filter it – a trick that also works for device selection.
It’s common to want to get an overview of how some code works. If you press Ctrl+Cmd+Up Xcode produces a generated interface, showing properties, function signatures, and comments. If you press it again, Xcode takes you to the tests for that file if they exist.
One of my most commonly used Xcode shortcuts is Cmd+/ to toggle comments for the current line or selection, but another useful one is Option+Cmd+/ – press that directly before a method to have Xcode generate a documentation comment, including its parameters.
As your projects grow, it gets harder to use the project navigator to find something. There are two solutions: one is to right-click on any file group and choose Sort By Name to arrange them A-Z, and the second is to type into the filter box at the bottom.
Xcode can handle source changes right in its editor. To try it, enable Show Source Control Changes in Xcode’s preferences, then make a change. On the left you’ll see a blue bar, but if you click it and select Show Change Xcode shows you the old and new code inline.
Xcode’s minimap helps us browse long files – enable it from Editor > Minimap. As you hover your mouse over it, the minimap tells you the name of the method you’re over. You can also hold Cmd to reveal the names of everything, then click on a name to jump to it.
It’s common to write failing tests, particularly when using TDD. Fortunately, Xcode has a keyboard shortcut to run only the last test, which is faster than running everything: Ctrl+Opt+Cmd+G. Jon Reid has a name for this making it easier to remember: “smash go!”
Some great shortcuts (e.g. Shift+Cmd+O for Open Quickly) are next to useless shortcuts (Shift+Cmd+P, for the never times you want to print code.) It takes only seconds to remove unhelpful keys, and you can even remap things like Cmd+P to resuming SwiftUI's preview.
All the titles in Xcode's find navigator can be changed: Find can Replace, Text can be references or regular expression, and Containing can be matching, starting, and more. You can also click the magnifying glass to see recent searches – selecting one repeats it.
If you’ve copied code from elsewhere, such as Hacking with Swift or Stack Overflow, there’s a good chance you’ll paste in something with incorrect indentation. Xcode can fix this with one shortcut: select the code you want to fix, then press Ctrl+I to reindent it.
You can test in-app purchases without App Store Connect. Make a new StoreKit Config File, and add your IAP. Now go to the Product menu, hold down Option, and click Run. From the Options tab of the window, change StoreKit Config, and now you'll use the test IAP.
There are many settings to control how Xcode builds your code, but it’s hard to remember what they all do. Fortunately, you can select one and use the Quick Help inspector to see documentation for most settings, or hold Option and double-click to get inline help.
The canvas is a great way to preview layouts as you work, but it’s harder to use when one view is split across files. To make things easier, use the pin at the bottom to keep the current preview active, allowing you to change one file while previewing another.
What are your favorite Xcode tips? Tweet me @twostraws and let me know!
And if you’re keen to learn even more Xcode tips, I have other articles that will interest you:
SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.