Custom Xcode shortcuts

People call me lazy but they just don’t understand, I’m just efficient! Or that’s what I keep telling myself.

A few weeks back I decided to optimize my workflow as much as possible and, since Xcode is the application I spend most of my time in front of, I decided to start with it. After writing a bunch of Ruby using TextMate back when I was working at ShopKeep, I got really used to a few keyboard shortcuts that are missing in Xcode. I’m talking about “Delete current line” and “Duplicate current line”. Pretty self-explanatory.

I also find Vim’s “Insert line below” (o) and “Insert line above” (O) really useful too. Let’s say the cursor is in the middle of a line and you want to enter a new line below. You have to go to the end of the line and hit return or similar, but that’s just too many keystrokes. With these shortcuts you can get that in a single keystroke including the right indentation of the new line.

Let’s see how to get these shortcuts (and any other) into Xcode:

First we need to modify a plist file that lives inside the Xcode.app bundle (needs sudo to edit)

/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist

Add these few lines inside the section “Insertions and Indentations”

<key>Insert New Line Below</key>
<string>moveToEndOfLine:, insertNewline:</string>
<key>Insert New Line Above</key>
<string>moveUp:, moveToEndOfLine:, insertNewline:</string>
<key>Duplicate Current Line</key>
<string>selectLine:, copy:, moveToEndOfLine:, insertNewline:, paste:, deleteBackward:</string>

and these inside the section “Deletions”

<key>Delete Current Line</key>
<string>selectLine:, delete:</string>

Now restart Xcode.

Open Xcode preferences and go to the “Key Bindings” tab. Use the search bar to filter and look for the new shortcuts. You can double click the desired shortcut to define a key binding. These are mine:
– Delete current line: ctrl + shift + k
– Duplicate current line: ctrl + shift + d
– Insert line below: ctrl + o
– Insert line above: ctrl + shift + o

Boom! You should be good to go.

As I mention, you can define any arbitrary shortcut using operations like “moveUp:”, “copy:”, etc. Just take a look at the plist file to see most of the available actions. The avid observer will notice that these actions look like an Objective-C selector and, in fact, they are. The actual implementation of these shortcuts relies on sending a selector defined in a plist to an object, pretty clever. For a complete list of selectors check this Xcode class dump, in particular the classes DVTTileView.h and HFTextRepresenter.h

Follow me on twitter for more tips like this

Advertisements

Introducing Nocilla: Testing HTTP requests in iOS and OS X

I’m happy to introduce my next open-source project. Nocilla is a testing framework that will allow you to stub HTTP requests for testing your iOS and OS X apps. It has a nice DSL that will make your tests super easy to understand and maintain and it’s super easy to use.

Let say that your module will GET google.com and you want to cover your ass in case google.com returns a 404 (hey, nobody’s perfect):

stubRequest(@"GET", @"http://www.google.com").andReturn(404);

NSAssert([google isDown] == true);

This is how it works: You need to write a piece of code that will handle communication via HTTP (or HTTPS, Nocilla works with both). You want to unit-test this code and handle all conceivable errors with the network. With Nocilla you can isolate your code from the real network. Nocilla replaces the dependency of the network in your code allowing you to have full control over which HTTP responses your code gets. This way you can set network scenarios that will be impossible to recreate with the real network. Nocilla is also way faster than the real network, speeding up your tests considerably.

The nicest part of Nocilla is its DSL, heavily inspired by WebMock. It’s a fluid interface in pure Objective-C (and yes, no square brackets). I’ll write a post on how I implemented the DSL because I’ve never seen this kind of API in Objective C and I think a lot of libraries will be able to take advantage of the powerfull technique I came up with.

The one limitation of the library is that it only works with NSURLConnection-based HTTP requests (AFNetworking, MKNetworkKit, NSURLConnection of course…). Nocilla has a pluggable architecture, so it’s super easy to create new hooks for other libraries like ASIHTTPRequest. In fact I started with ASIHTTPRequest and I shifted the work toward NSURLConnection. The hook for ASIHTTPRequest should not be too hard and it will be a “nice to have”, especially to help people migrate to other HTTP libraries (ASIHTTPRequest is no longer maintained).

Please note, Nocilla will block any HTTP request by design. If an HTTP request occurs while Nocilla is enabled and it’s not expected (not stubbed using the DSL) that request won’t hit the real network. After all, your unit tests should not hit the real network. An option will be available to enable real HTTP requests, but it’s not advised.

I hope you like it and use it. Contributions are also more than welcome.

Nocilla in Github | https://github.com/luisobo/Nocilla