News Tips & Tricks

Complete Statement in AppCode

The are several completion types available for Objective-C in AppCode. Basic completion (^Space) is an essential IDE feature that allows you to quickly complete variable, class and method names as you type. Smart Type completion (⌃⇧Space) is more advanced: it analyzes the context you are currently working with and filters the list of methods and variables to match the expected expression type.

The latest AppCode EAP delivers a new kind of completion: Complete Statement (⇧⌘Enter). Let’s take a look at how it can speed up writing Objective-C code together with some other AppCode features.

We will implement a simple cellForRowAtIndexPath method for the UITableView in the UIViewController. First step is to dequeue the table cell and introduce the needed variable:
Step 1
Note that you do not need to care about square brackets in this case – AppCode will insert them automatically.

For this sample let’s use a formatted NSString as a unique cell identifier. Invoke Smart completion and quickly insert [NSString stringWithFormat]:
Step 2

After specifying the id, do not move the caret using the keyboard or mouse – just invoke Complete Statement, and the semicolon will be inserted automatically.

To get the cell instance, let’s create a local variable using Introduce Variable (⌥⌘V) and then use Complete Statement to quickly jump to the new line:
Step 3

As usual, we should check if the cell was already created. And here goes another use-case for Complete Statement: type if and finish completion with ⇧⌘Enter to jump into the parentheses for specifying an expression, and one more time to jump inside the curly braces:
Step 4

Complete Statement can also be used with other control statements, such as for, while, do and switch.

Invoke Smart Completion to allocate the cell and specify the cell type after it:
Step 5

Oh, wait! We need the cell identifier again, and there is no sense to have a duplicate in the code. Let’s separate it into a variable and then re-use it:
Step 6

Move the caret to the initWithStyle method, jump to the second parameter using Tab, invoke Basic Completion and finish it with Complete Statement again:
Step 7

Now we can return the cell from the method – invoke Basic Completion and finish it with Tab instead of Enter:
Step 8

That’s it! Since Complete Statement functionality is rather new, there may be bugs (stable version is planned for AppCode 2016.2). Also, for now it’s available only for Objective-C and C++, because for Swift we need to finish more important tasks related to completion which are currently in active development.

Anyway, feel free to share invalid cases with us so that we can fix them before the release. Thanks!

Your AppCode Team
The Drive to Develop