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 class) that doesn’t exist yet and hit
⌥⏎ 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
That’s not it! Say, you declare a property: hit
⌥⏎ and AppCode will synthesize it.
An instance variable needs to be moved to implementation? Need getter and setter for a property? Flip a binary operation?
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.
Another way to generate constructs is using
⌘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 the 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
⌘N from Project View, AppCode suggests you the different set of options: you can generate a new class, protocol, category or another 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:
1) Implement methods with
2) Override methods 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 (by the way, when doing that, try using
⌥↑: 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
⌥⌘T, and choose
Whenever you need an
((Type)expr) or something else – you can add it with
Live templates enhance Xcode code snippets by providing more functionality and easier management. Type
each and hit
⇥ to iterate a collection, or
⇥ 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
⌘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
That’s it for today, I hope you’ve learned something new. Stay tuned!