AppCode Code Generation 101

When you’re in the creative mood, developing a new app, or adding cool features to your current one, you definitely don’t want anything to slow you down, right? What probably bugs the most is being stuck by typing each and every thing.

Naturally, there is code completion, but on top of that a lot of code constructs can be simply generated. AppCode comes with a wide set of code generation options, and it would take more than one post to cover them all, but I’d like to make a quick overview of them so you’d get the idea of the power behind it, and we’ll get back to them in detail later.


You know why intentional programming is such an awesome thing? I have a simple answer – because we all are lazy, some more, some less. I bet you know how tempting it is to start writing code using methods, classes, variables and whatever else, that doesn’t exist yet. So why holding yourself back?
AppCode lets you be as lazy as you want to. You know those lightbulbs in the gutter area that tell you right away when there’s an error in your code? In fact not only they help you fix problems, but there’s also a whole set of intention actions. Type a name of a method (or a class) that doesn’t exist yet and hit Alt+Enter to create it (or click the lightbulb in the gutter area).

When you generate a method this way and you want it to be declared in a private category, place the caret at method definition and hit Alt+Enter.
That’s not it! Say, you declare a property: hit Alt+Enter and AppCode will synthesize it.
An instance variable needs to be moved to implementation? Alt+Enter
Need getter and setter for a property? Alt+Enter
Flip a binary operation? Alt+Enter
Localize a string? You know the shortcut, right? To see the whole list of available intention actions, just open Preferences | Intentions. Honestly, this shortcut is one of the most important ones, and I can’t stress it enough.

Generate (Cmd+N)

Another way to generate constructs is using Cmd+N (or Code | Generate from the main menu). Here you can choose to create initWith and objectWith methods, declare and synthesize properties, declare members in header file, etc. Most of these things are also available as intentions actions, which one to use is truly more the matter of taste and preference.
By the way, if you invoke Cmd+N from Project View, AppCode suggests you different set of options: you can generate a new class, protocol, category or other file.
Since two of the options are used much more often than others, and I’m talking about “implement/override methods”, they have their own shortcuts to let you reach them even faster: Cmd+I and Cmd+O.

1) Implement methods with Ctrl+I

2) Override methods with Ctrl+O

Surround With

Say, you have an expression or a set of statements you’d like to put inside an “if” statements, what’s the fastest way to do that? First, select what you want to be surrounded with “if”. (Btw, when doing that, try using Alt+Up: with each hit it expands current selection to the next widest token, I’m sure you’re going to love this one.) Now, back to our “if”. Just hit Alt+Cmd+T, and choose “if”.

Whenever you need an “if”, or “for”, @try/@catch, while, (expr), ((Type)expr) or something else – you can add it with Alt+Cmd+T.

Live Templates

Live templates enhance Xcode code snippets by providing more functionality and easier management. Type each and hit Tab to iterate a collection, or logm and Tab to log current method. The whole list of live templates you can find under Preferences | Live Templates, and when you don’t remember the acronym for the construct you need, just press Cmd+J to see the list of suggested templates.
Under Preferences you can even add your own live templates, for example a new “surround with” using $SELECTION$ variable, and it will be available in surround with options when you hit Alt+Cmd+T.

That’s it for today, I hope you’ve learned something new. Stay tuned!

This entry was posted in Tips'n'Tricks and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *