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!
Subscribe to Blog updates
Thanks, we've got you!
Writing Code Documentation in AppCode
Code documentation can save you a lot of time and headaches. On the other hand, it also takes a lot of effort to write and maintain it. In this article, we will look at the AppCode features that can help you create detailed and well-structured code documentation and keep it updated after code refact…
Polish Your Code with AppCode’s Inspections and Intentions
AppCode is constantly analyzing your code to find parts that can be improved. It provides sets of code inspections and intention actions for each supported language for this exact purpose. Inspections locate unused or redundant code, syntax errors, probable bugs, spelling mistakes, obsolete or redun…
Navigation in AppCode
Navigating through files and source code, especially in complex projects, can eat up a lot of time, which is why it is essential to learn how to move around quickly in your IDE. AppCode provides plenty of navigation features that can help you move faster. In this blog post, we’ll take a look at some…
Server-side Swift in AppCode
In this short article we’ll take a look at various AppCode features that are useful for server-side Swift development, as now you can open standalone SPM projects in AppCode on macOS. Check out and open the project On the Welcome screen, click the Get from VCS button and enter the address of the Fe…