Xcode is a program with so many options and features. Investing some time into learning some of the shortcuts and tricks will save you time in the long run. I’ve been a professional iOS developer for a couple of years now, so I’d thought I’d share some of my productivity tips in Xcode and Swift.
This opens your current file in the project navigator. It’s one of my most used shortcuts. Opening the current file in the project navigator allows you to browse files that are in the same folder which you’ll likely need. This enables you to browse through the codebase more easily without getting lost. If your project navigator is closed, this command will also open it up.
This keyboard shortcut executes the clean build folder command on your project. Sometimes your project is not building for one reason or another. I always run this command before I start debugging or diving into the potential problems. I’d say 95%+, build problems can be resolve just by running a simple clean. This command will save you a lot of headaches and time!
This shortcut is handy for navigating quickly to the top of the file or the to bottom of the file. It helps when working with smaller code files. Learning these shortcuts can improve how fast you read code.
As you write more code, you’ll develop patterns of having certain functions near the bottom of the file and properties near the top.
Create a quick documentation style comment template for you to fill in. This is useful to document complicated functions. Don’t over use these comments, ideally your function’s name and its parameter names should explain themselves. Try to use this for only complicated functions.
I use this shortcut all the time. It comments out the line you have highlighted. Great for debugging by removing certain sections of code or to test how something will behave with blocks of code commented out.
This keyboard shortcut opens up Inspector Panes. I use this command a lot when I’m working inside the interface builder. This tip allows me to modify attributes of the UI elements I am working on quickly. Honestly, I don’t remember how to open this menu up without the short-cut anymore :).
I use CMD + Option + 4 the most often as this is the Attribute Inspector pane. This pane has most of the options I use when working inside the interface builder.
This shortcut starts the Build and Run command. Probably one of my most often used shortcuts as well. You’ll be executing this command a lot while you’re debugging or building code.
This is the Find in Workspace command. If you’re working in anything other than a small codebase, you’ll find this search shortcut extremely valuable. This enables you to navigate through the codebase at a much faster space. The larger the codebase, the more useful you’ll find this command.
These shortcuts will navigate through the different navigator panes. They’re number 1 through 6. I find myself using Cmd + 1 the most to open up the Project navigator.
This command creates a new file and is typically of many apps, not just Xcode. This saves me some time when I’m creating a new Swift file or a new nib file.
Opens up the UI Object library in the interface builder. This allows you to select different objects in the UI library such as labels, buttons and other interface controls. This shortcut is useful when you’re trying to build user interfaces very quickly.
This opens up the UI Object library but also persists it in its own separate window. Useful when you want to keep this window open, just like older versions of Xcode.
Shows your media library allowing you to add images quickly to your user interface. Also allows you to search through your media library to see what you have available.
This command will jump to the definition of any function or variable. This is again useful when navigating through large codebases. I use this command a lot when I’m learning a new part of the codebase or debugging an existing feature.
This keyboard shortcut will hide or show the debugger area. The debugger is an essential feature for most developers as it greatly speeds up debugging. I like to hide away this section when I’m reading code.
This shortcut allows you to quickly jump to a line in the current file. This is helpful when you’re working on a team and somebody specifies a certain line number they want you to look at.
Creates a new tab where you can have another editor window. Tabs can be later pulled into separate windows if desired.
Navigates through your tabs forward or backwards.
This will open up a documentation fly out. This allows you to quickly read and understand what a function call does. If you like to read more there is an option to open up the full developer documentation.
Hold option and hover over any bracket. This will quickly highlight all the code between the open and closing bracket. Super useful when working within many nested brackets.
Quickly opens a file in the assistant editor when you option-click a file in the project navigator. Super useful when you need to compare two files quickly.
Quickly open any file in your project. One of my favorite shortcuts! Especially useful if you’re working in a very large codebase.
You ever have some code that’s been all messed up indentation wise? This command will quickly fix the indentation making your code pretty and more readable.
If you want to rename a variable or function, this shortcut will allow you to edit all instances of it at the same time.
This brings up the keyboard in simulators. It’s important to test how your application looks like when the keyboard is on the screen. A common bug is having critical user interface elements hidden behind the keyboard. This can lead to a very frustrating user experience.
This rotates your simulator in a clockwise or counter-clockwise direction. This is critical to ensure your application performs as expected in both orientations. Sometimes user interface elements can be misaligned or in the wrong position when changing orientations. Remember, the user can rotate their device at any time while your application is open.
Have you found yourself rewriting the same code over and over again? Code snippets are your answer to saving some time. It’s under Editor > Create Code Snippet.
You can then assign an autocomplete keyword to have the code autocomplete every time you type this code out.
To create a warning or error in your code, just use #warning or #error. If you put a warning in, it will show up in your Issue Navigator, allowing you to keep better track of it.
If you use an error, your build will not compile. Here are some code examples:
#error ("this is an error") #warning ("this is a warning")
In Xcode’s search tab, you can create a new Scope. This new scope allows you to filter your search down. A very common filter I use is files ending in .swift or .m. This allows me to search all Swift files or all Objective-C files separately.
Another search tip is to use the filter text field at the bottom of the search results. I didn’t find out about this until a couple months into iOS development. It’s super useful to filter your current search results quickly instead of having to perform a new search every time.
This tip is more useful for iOS developers working on large codebases where there can be a lot of warnings. With all the warnings it can be hard to see the errors and what is causing your build to fail. There’s a small but very useful button at the bottom of this pane which allows you to hide all warnings and show only errors. Next to that button, there is another icon that allows you to show only issues from the latest build.
It’s important to make sure your user interface appears correctly on different screen sizes. Xcode makes this easy by allowing you to view your layout in different screen sizes. In the interface builder, theres a pane in the bottom left of the main editor window. In this pane, you can select different devices including all kinds of iPhones and even iPads. There are also options to view both orientations so you can be sure your app looks as expected in portrait and landscape mode.
Refactoring in Xcode has been made easier over the last couple versions. Now when you’re renaming a function, Xcode can change all the references to that function automatically. This also works for variables and class names. After you’ve highlighted the name of the object you want to refactor, simply right click and select the Refactor > Rename option.
You can extract function and refractor it by highlighting the function, right-clicking and selecting extract method. Xcode will automatically create an extracted function for you and call it in the method. Then you can rename the function as required.
Adding a // MARK: in your code will allow you to break up your code into different sections. This will allow you to organize your code better in a single file.
Using the split screen assistant editor makes it easy to work with two files at once. This is especially useful when connecting IBOutlets to their respective Swift. It’s also useful when you’re trying to compare to different files and determine if you can refactor them or not.
To access the split screen editor, select the icon that looks like two circles near the top right hand corner of Xcode. To go back to a single editor, select the Icon that looks like a bunch of lines.
When you get orange warning lines in your Interface Builder, that means some elements are not where they should be according to the constraints you’ve set. You can manually move these elements into their correct position. However, this can be particularly frustrating every time something is a couple of pixels off. Moving these elements to their exact correct location seems like a waste of time. There is a better way to do this however and it’s as simple as clicking the update frames button at the bottom right-hand corner.
When you click this button Xcode will automatically move all elements to their correct position according to the constraints you’ve set.