XCode is a huge program, and if you can even use a couple of tricks to make your work more efficient, you can save a few hours every week. In this article, I want to introduce you to 24 Xcode tips that will help you code faster, create better tests, and navigate large projects more efficiently—I'm sure there's something for everyone!
1. Generation of class initializers
Swift can synthesize memberwise initializers for structures, but not for classes. Luckily, XCode can generate code for us: select your class name, then go to the Xcode menu and choose Refactor
> Generate Memberwise Initializer
. Ready!
2. Redefining the environment
When building software, it's important to make sure your application works for everyone. A great way to do this is to use XCode's Environment Overrides panel, which allows you to configure dynamic type size, accessibility options, and appearance in one place.
3. Choice of code blocks
You probably already know that you can click after any opening {
or closing }
brace to have XCode highlight the corresponding brace, but another good tip is that you can double-click on a brace to select the entire block of code, which it contains.
4. Checking our spelling
XCode can check spelling and even understands that camelCaseNames should be treated as separate words. Go to the XCode menu and select Format
> Spelling and Grammar
> Check Spelling While Typing
and Xcode will check your code for typos.
5. Correction of all problems
If you have a Swift file that contains a lot of errors, you can use XCode hints to try and fix the errors one by one. But a faster way is to go to the menu and select Fix All Issues
to apply all the fixes at once. (Make sure and double-check what changes have been made!).
6. Deleting search results
When you search using XCode's search navigator, you can click to view individual results and make any changes you want. But when you're done, I recommend that you press Backspace to remove that search item from the results so you can see what results are left to check.
7. Resuming the SwiftUI Canvas
This is the easiest tip I know, but it will save you time if you don't already know it. Whenever you see the preview auto update pause on the SwiftUI canvas, press Opt+Cmd+P to resume the preview. Or use Opt+Cmd+Return to completely hide the canvas.
8. Simulator tiling
If you often switch from XCode to the simulator, place them next to each other. With the simulator active, go to the menu window and select Tile Window To Right Of Screen, then select Xcode on the left. Adjust the position.
9. Autocomplete Extension
XCode has great code completion built in, but sometimes when you're scrolling through the options, you may find that the names are too long to fit. Luckily, you can just grab the edge of the autocomplete popup and drag it as wide as you want!
10. Fast switching breakpoints
Breakpoints are great for debugging - place them by selecting the line number, or remove them by right-clicking and choosing Delete Breakpoint. You can also remove one by dragging it, but a quick way is to use Cmd+\
to toggle the breakpoint on the current line.
11. Test Order Randomization
Sometimes the output of one test affects the input of another. XCode has a solution: go to the Product menu
, hold down the Option button
, then click Test
. On the Info tab
, click Options
, then check the Randomize Execution Order
checkbox to run the tests in a different order each time.
12. Jump bar filtering
Jump bar in XCode (Ctrl-6
) is the best way to quickly navigate through long files, allowing you to see all the properties and methods in your file. For longer files, start typing with the jump bar open and Xcode will filter them out - a trick that also works for device selection.
13. Interface file generation
Often you want to get an idea of how some code works. If you press Ctrl+Cmd+Up, XCode will create a generated interface showing properties, function signatures, and comments. If you click it again, XCode will take you to the tests for that file, if they exist.
14. Quickly add comments
One of my most used XCode keyboard shortcuts is Cmd+/
to toggle comments on the current line or selection, but another useful one is Option+Cmd+/
- press it just before a method to have Xcode generate a documentation comment, including its options.
15. Find files faster
As your projects grow, it becomes more and more difficult to use the project navigator to find something. There are two solutions, one is to right-click on any group of files and select Sort By Name to sort them alphabetically, and the other is to type in the filter box at the bottom.
16. Display initial changes at runtime
XCode can process source changes right in its editor. To try it, turn on Show Source Control Changes in XCode Preferences and then make your changes. You'll see a blue bar on the left, but if you click on it and select Show Change, Xcode will show you the old and new inline code.
17. Search code on the minimap
The XCode minimap helps us view long files - turn it on in the Editor
> Minimap
menu. When you hover your mouse over it, the mini-map tells you the name of the method you've completed. You can also hold Cmd
to show the names of everything and then click on the name to jump to it.
18. Rerun the last test
It often happens that bad tests have been written, especially when using TDD. Luckily, XCode has a keyboard shortcut to run only the last test, which is faster than running everything: Ctrl+Opt+Cmd+G. John Reed has a special name that makes it easier to remember: "Smash Go!"
19. Remapping useless keys
Some great keyboard shortcuts (like Shift+Cmd+O
for quick opening) sit next to useless keyboard shortcuts (Shift+Cmd+P
when you want to type code). It only takes a few seconds to remove useless keys, and you can even remap shortcuts like Cmd + P
to resume SwiftUI preview.
20. Improve your searches
All headings in the XCode search navigator can be changed: Find can be "Replace", Text can be a link or regular expression, and Containing can be a match, start, etc. You can also click the magnifying glass to view recent searches - when choosing one of them, it repeats.
21. Change code indentation
If you've copied the code from somewhere else, then there's a good chance you'll paste something with the wrong indentation. XCode can fix this with one shortcut: select the code you want to fix, then press Ctrl+I to change its indentation.
22. Testing in-app purchases
You can test in-app purchases without App Store Connect. Create a new StoreKit config file and add your IAP. Now go to the Product menu, hold down the Option key and press Run. On the Options tab, change the StoreKit configuration and you will now use the test IAP.
23. Explore build options
There are plenty of settings to control how XCode builds your code, but it's hard to remember what they all do. Luckily, you can select one and use the Quick Help inspector to view the documentation for most of the options, or Option-click and double-click to get the built-in help.
24. Pin preview
Canvas is a great way to preview layouts as you work, but it's harder to use when one view is split into multiple files. To make things easier, use the pin below to keep the current preview active, allowing you to change one file while previewing another.
Top comments (0)