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.

Share
Source: http://tuaw.com/tag/devjuice
Categories
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...
Add a Comment
This is a great feature, yet for some reason some of my autocomplete shortcuts along with the system's do not show any sign of completion. Even a simple "switch" completion in C doesn't generate any results. Anyone has any ideas?
July 06 2011 at 6:34 AM Report abuse Permalink rate up rate down ReplyFigured it out. Those code snippets' completion scope is set by default to "Function or Method" and would only work in Objective-C documents. It does not recognize those in C. Guess I'll make a custom one myself.
July 06 2011 at 8:02 AM Report abuse Permalink rate up rate down ReplyThat is a really cool tip! Thank you very much.
July 05 2011 at 12:24 PM Report abuse Permalink rate up rate down ReplyHot Apps on TUAW
Deals of the Day
more deals- Used Apple iPhone 3G 8GB for AT&T for $108 + $5 s&h
- Apple Mac Pro Xeon 6-Core 3.3GHz Desktop w/ 12GB RAM for $3,899 + $28 s&h
- Apple MacBook Pro Core i7 Quad 2.2GHz 15" SSD Laptop for $2,447 + $13 s&h
- Apple Earphones with Remote and Mic for $6 + $2 s&h
- PC Micro Store sale: Up to 50 off
- USB MP3 Player FM Transmitter with remote for $6 + free shipping
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



4 Comments