Supercharging Your Xcode Efficiency

Boost your XCode efficiency and learn how to become a coding ninja by following this tutorial. By Jack Wu.

Leave a rating/review
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Above and Beyond

Now that you have a functional app and a happy designer, now you just need to do a little code cleanup.

Use Open Quickly to open CTCardCell.m – you should know how by now! Remember to enter Dev Mode as well.

Just look at that messy list of @properties at the top of CTCardCell.m:

@property (weak, nonatomic) IBOutlet UILabel *locationLabel;
@property (strong, nonatomic) NSString *website;
@property (weak, nonatomic) IBOutlet UIButton *fbButton;
@property (weak, nonatomic) IBOutlet UIImageView *fbImage;
@property (strong, nonatomic) NSString *twitter;
@property (weak, nonatomic) IBOutlet UIButton *twButton;
@property (weak, nonatomic) IBOutlet UILabel *webLabel;
@property (weak, nonatomic) IBOutlet UIImageView *profilePhoto;
@property (strong, nonatomic) NSString *facebook;
@property (weak, nonatomic) IBOutlet UIImageView *twImage;
@property (weak, nonatomic) IBOutlet UILabel *aboutLabel;
@property (weak, nonatomic) IBOutlet UIButton *webButton;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

In this section, you’re going to create a custom service to run the shell commands sort and uniq on blocks of code like this.

Note: If you’re not familiar with these shell commands, they’re quite self-explanatory. sort organizes the lines alphabetically, and uniq removes any duplicate lines.

uniq won’t really come in handy here, but is handy when you’re organizing #import lists!

Mac OSX allows you to create services you can access throughout the OS. You’ll use this to create a shell script service to use in Xcode.

Follow these steps to set it up:

  1. In OSX, search for Automator using Spotlight and open it
  2. Go to File\New and choose Service
  3. In the Actions filter bar, type in shell and double-click on Run Shell Script
  4. In the bar above the newly added service, check Output replaces selected text
  5. Change the contents of the script to sort | uniq
  6. Press Command S and save your new service as Sort & Uniq

Here’s what the final window looks like:
SortnUniqSS

Now Go back to Xcode and select that messy block of @properties in CTCardCell.m. Right click on the selected code and go to Services -> Sort & Uniq and watch how tidy that rowdy list becomes. You can watch the magic on the big screen here:
SC6-SortUniq

Now that is worth at least 800 coolness points.

Stats:

Coolness points gained:801

Total Coolness points:2201

Ninja points gained:0

Total Ninja points:400

Code Snippets

That marks the end of basic ninja training and your task of debugging CardTilt – congratulations on getting here! I hope you’ve learned and feel more cool and ninja-like.

Surely, you’re eager to learn even more tricks. Fortunately for you, there is one last trick to share.

You have likely used Xcode’s Code Snippets before. Some common ones are the forin snippet and dispatch_after snippet.

In this section, you’ll learn how to create your own custom snippets and look extremely cool as you re-use common code blocks.

The code snippet you’ll create is the singleton accessor snippet.

Note: If you’re not familiar with the singleton pattern, you can read all about it in this great tutorial.

Below is some boilerplate code you’re likely to use frequently with this pattern:

+ (instancetype)sharedObject {
  static id _sharedInstance = nil;
  static dispatch_once_t oncePredicate;
  dispatch_once(&oncePredicate, ^{
    _sharedInstance = [[self alloc] init];
  });
  return _sharedInstance;
}

What’s also cool is that this snippet includes the dispatch_once snippet.

Create a new class in CardTilt called SingletonObject and make it a subclass of NSObject. You won’t actually it for anything, except for as a spot from which to drag code to create a snippet.

Follow these steps:

  1. Paste the code above into SingletonObject.m, just below the @implementation line
  2. Open the Code Snippets Library using Command Option Control 2. You should see the library of code snippets that are included in Xcode by default
  3. Select the entire +sharedObject function and drag it into the library

Note: If you’re having issues dragging code, click on the selected code and hold for a second before starting to drag.

Here is what this looks like:
snippet

Your new code snippet will automatically show up at the very bottom of the library. You can use it by dragging it from the library into any file – go try it out!

Now double-click on your newly created snippet and press edit.

The fields that display in this popup are particularly useful; in fact they are so valuable that each deserves and explanation:

  • Title and Summary: The name and description of the snippet that displays in the library and during completion.
  • Platform and Language: The platform and language that this snippet is compatible with.
  • Completion Shortcut: The code completion shortcut you can type in Xcode to this snippet.
  • Completion Scopes: The scope in which this snippet should be available via code completion. This is great for maintaining a clean snippet library.

Fill in the properties like this:
SS-Snippet

Tokens

Snippets become especially powerful when you add Tokens because they allow you to mark code in the snippet that shouldn’t be hard-coded. It makes them very easy to modify using the Tab key, much like as it is with auto-completed methods.

To add a token, simply type in your snippet.

Create a token for the Object part of your sharedObject snippet by replacing sharedObject with shared so it looks like this:

SS-Snippet2

Save the snippet by hitting Done and give it a spin.

Type singleton accessor in the SingletonObject implementation file and use the autocomplete when it shows up.
SC7-Snippet

Custom code snippets like this can become very powerful for frequently used patterns. Learning these last few tricks is definitely worth some extra points!

Stats:

Coolness points gained:50000

Total Coolness points:52201

Ninja points gained:2000

Total Ninja points:2400

Where to Go From Here

Congratulations on achieving such a high score!

To sum it up, here’s what you’ve learned and done as you’ve worked through this tutorial:

  1. Modified the layout of Xcode using hotkeys.
  2. Made Xcode change its layout for you based on behaviors you defined.
  3. Made use of the assistant editor.
  4. Learned to use Open Quickly to navigate through Xcode.
  5. Deleted results from the Find navigator.
  6. Made your designer happy by aligning views in the Interface Builder with guides and hotkeys.
  7. Created a Mac OSX service to use in Xcode.
  8. Created and used custom code snippets.
  9. Most importantly, you learned how to become an Xcode ninja.

That was all pretty easy, now wasn’t it? Think of all the cool tricks you have to show your friends and family now! They’ll no doubt completely understand your excitement ;]

There are still plenty of other ways you can improve your Xcode efficiency and up your own personal coolness and ninja factors. A few of them are:

The next step is to go and put your newfound ninja skills to use!

I hope you enjoyed this tutorial. If you have any questions, comments or would like to share a cool trick you know, make sure to leave a comment below!

Jack Wu

Contributors

Jack Wu

Author

Over 300 content creators. Join our team.