5 Time Saving Tips In Xcode

Working with Xcode can sometimes be a frustrating experience…

However, Xcode has a few times saving features that really make a difference in day to day development. In this post we are going to break from the traditional format in order to examine a few of these features.

1. Snippets

Xcode snippets have gotten a bad wrap since they were introduced. This is mostly because they seem to lack some obvious features and are poorly documented. However, this doesn’t stop them from being an immensely powerful addition to Xcode. So let’s dive into using existing snippets and creating new ones.

Creating a basic initialization method is a task that every developer does on a regular basis and becomes second nature boilerplate. Xcode has a built in snippet to handle the basic case for you! All you have to do is type init within the implementation scope of your class and press return. This will create an init stub as shown below.

1
2

So where did this come from?

Xcode Snippets! In Xcode, from the menu bar browse to View > Utilizes > Show Code Snippet Library or press control + option + cmd + 2 to expose the code snippet library in the bottom right. There are many other great snippets supplied by Xcode along with the init stub we just showed, such as typedef’s, coreData helpers, GCD helper’s and more.

For example typing dispatch_after will add the following code block for dispatching with a delay.


double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    //code to be executed on the main queue after delay
});

Or typing dispatch_once will add the code block for dispatching a block once. An essential for singleton accessors!


static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    //code to be executed once
});

So the built-in’s are great, but what about creating a few of my own? This is where the real benefit is, right? Of course! let’s create a few essential snippets of our own, starting with the pragma mark. If you’re anything like me, you probably hate typing pragma mark all the time even though it’s necessary in order to organize your code effectively.

  • Start by typing the code you want generated when you call your snippet

#pragma mark -
  • Select all the code to be added to the snippet, then click and hold for a second (the text will then become draggable). Once the text is draggable, go ahead and drag it onto the Snippets Library. You will then get a popup to finalize the snippet creation as shown below.

3

  • Finalize the snippet creation by filling out the fields in the popover
    • Title Add a meaningful title such as pragma mark
    • Summary provide a summary if needed, we will skip this one for the pragma mark
    • Platform select whether the snippet should be iOS or OS X only, or both. We will use both
    • Language select the programming language this snippet should belong to. We use Objective-C
    • Completion Shortcut this is the value you will type to create the snippet, we will use mark
    • Completion Scope select the scope this snippet should be available in. For pragma marks we want All
  • Now select Done, and the next time you type mark in an Objective-C source file and press return you will have a pragma mark!

There is one more handy trick we didn’t yet add to the pragma mark snippet, and that is the input bubble. Wouldn’t it be better if the snippet came populated with the required input bubble like below?

4

Well, in more advanced snippets, this will be absolutely necessary, so let’s go ahead and add it to the pragma mark snippet.

  • Open the snippet be selecting it in the Snippets Library and waiting for the popover to show up, then select Edit.
  • To create an input bubble, you need to wrap the input text in angle brackets and pound signs. Here is an example

<#input text#>

  • For the pragma mark snippet, we will add <#name#> to the end of the snippet as shown.

5

That’s all you need to go out and create a bunch of time saving snippets to aid your daily development activities! Here are a few other snippets I would recommend for saving time.

logm To log a method


NSLog(@"%@ - %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd))

loc For localized strings


NSLocalizedString(@"<#string#>", @"<#message#>")

I would also take the time to add a bunch of other log snippets one for each type such as logr for CGRect or logf for a float.

2. Method Auto-completion

Xcode has a really cool feature that will auto-complete declared methods. This is one of my favorite features!

So let’s dive into a quick example.

Say I declare a method called reallyLong:andBoring:method:thatSucks:toType: in the header file of a class.


- (void)reallyLong:(id)arg1 andBoring:(id)arg2 method:(id)arg3 thatSucks:(id)arg4 toType:(id)arg5;

Now in the implementation of that class I can begin typing, really, and Xcode will complete the method. The key here is to skip the return type.

6

As a side note this is really helpful for protocol methods as well. For example, if you were to declare that ViewController from the above example conforms to the UITableViewDelegate protocol, then Xcode will auto-complete those delegate methods in the same fashion as shown above.

3. Open Quickly

option + cmd + O

If you don’t know about it or haven’t really used it much then you are wasting a lot of time. Open quickly allows you to easily search for, and jump to, various source files using a single search field.

Here it is in action.

Say I am looking for the source file where reallyLong:andBoring:method:thatSucks:toType: is declared. First you would start by pressing Option + cmd + O to open the Open Quickly dialog. I would then start typing “really…” and the following result appears.

7

All you then have to do is press enter and Xcode will take you directly there! Amazing stuff!

4. Use Pragma Marks

Add pragma marks in your source code files, you will be thanking yourself in the future. By separating logical parts of code with pragma marks, you will be making your code much easier to read and debug. Below is one example of a basic pragma mark structure. Notice, methods inherited from UIViewController, are in the UIViewController section, accessors are labeled Accessors, and public methods are labeled Public.

8

It is very easy to see how this structure can help organize the code, but Xcode will also help you save time if you use this structure. For example, pressing control + 6 will bring up a method browser for the source file, that has been nicely separated based on your pragma marks. You can easily browse this and select a particular method to jump directly to it in code.

9

Another time saving benefit to this system is taking advantage of real class or protocol names such as we did with the UIViewController section above. This allows us to browse directly to the UIViewController header file by cmd + clicking or quickly jump to the docs for UIViewController by option + clicking. This is an awesome time saver when implementing protocols. By using the convention below


#pragma mark - protocolName

you can easily jump to that protocol for reference or to grab additional methods when implementing it! Combine this with a pragma mark snippet and you have real time savings!

Example for UITableViewDelegate

10

5. Keyboard Shortcuts

Finally, we get to keyboard shortcuts for which no time saving Xcode article could be without. Keyboard shortcuts can be a very personal thing, and 20 developers would give you 20 different answers to which are the best. For this reason I will only highlight a few that I think are absolute necessity.

cmd + option + arrow

This is the easiest way to navigate back and forth in Xcode. To go to the previous file you were on, use cmd + option + left arrow and to go forward use cmd + option + right arrow.

cmd + up arrow or cmd + down arrow

This keyboard shortcut will take you to either the top of the source file (cmd + up arrow) or the bottom (cmd + down arrow), very handy!

control + cmd + up arrow

This little ditty will switch between corresponding h. and .m files. It even works if you have a class with a private header file (assuming your naming convention is correct). For example if I have a custom class named CustomView and it has both a public header CustomView.h and a private header CustomViewPrivate.h and an implementation file CustomView.m, using control + cmd + up arrow will cycle through all three!

Edit All in Scope (control+cmd+E)

Edit All in Scope allows to you to highlight and change the name of all variables in a given scope. First, move the cursor to the variable you wish to edit, Xcode will add dotted lines to the bottom of all matching variables in scope. To edit all variables in scope, all you now have to do is press control + cmd + E.

Final Thoughs

Even though these are just a few simple ideas, using them can add up to a real time savings throughout the work week. Go ahead try them out, see if you notice yourself being more productive!

Now I know we probably missed a few time saving features or techniques, so please comment and tell us about what saves you time when developing in Xcode! Here at NSCookbook, we are always looking for ways to be more productive so we would love to hear from you.

Comments

  1. Thanks! I love snippets!

  2. To open the Open Quickly dialog press shift + cmd + O.

  3. These tips are great! My favorite is the init method autocomplete trick

  4. Go Back in fact goes back to the previous file that has been edited; it is so sad that the “real” nagivate back of Visual Studio (Ctrl-‘-‘ by default) functionality is missing from Xcode. Common, it exists even in Vim!

  5. Tushar Garg says:

    Thanks a lot. I stumbled on this blog while looking for a shortcut to generate empty definitions for methods declared in .h file.
    One shortcut that I use very frequently :
    ⌘ + ⇧ + 1 : To open project navigator.

Speak Your Mind

*

css.php
Privacy Policy