Back to Mobile View

Skip to Content

TUAW Deals

Dev Juice: How do I autocomplete in Xcode 4?

Dear Dev Juice,

I just moved from Xcode 3 to Xcode 4. How can I make autocomplete give me a template? I type -(void)deal and the drop down pops up. tab just completes the dealloc word.

I want it to pop out the template for me. Space bar doesn't work, enter, escape, nothing anymore. I need to know how to make it dump the template

Tigerpaw

Dear Tigerpaw,

Xcode 4 offers a slightly different take on text macros. Code snippets are now the way to go.

The problem is that you're typing -(void) deal and expecting it to expand to the skeletal dealloc method. What you need to do is this. Go outside of any method and type de. No "- (void)". Immediately, Xcode will pop-up a completion menu and the first choice will be dealloc.

That's because you need use code snippets by name in Xcode 4, not by structure. You can find out the name of your snippet by using the Code Snippet Library browser.

Enter dea in the text field at the bottom of the browser. It returns just one match, "Objective-C dealloc Method". Hover over the selected item until a pop-up appears and then click Edit. Here you'll find the completion shortcut name. As you can see, the name here is "dealloc".

Creating your own custom snippets and naming them with shortcuts is just as easy. Select text in the Xcode code editor. Click and hold for a second in the selection and then drag it over to the library. It appears at the end of the library list and is named My Code Snippet by default.

Do the hover/pop-up/edit trick again to enter the edit mode above. Change the snippet's title, summary, and completion shortcut and click Done. Your new snippet will now be available for use using the shortcut you assigned.

Enter the first few characters, press return. Presto.

Should you want to add a placeholder into your new snippet, surround it with <# and #>, e.g.

int foo = <#value#>;

Xcode automagically converts this into a placeholder in the editor.

© 2014 AOL Inc. All Rights Reserved.