Can I add my own code snippets to xcode 4?

CocoaXcodeXcode4

Cocoa Problem Overview


It'd be really handy to have collection KVC accessor snippets in xcode, as they're a pain to do by hand. Has anyone who's been using 4 for a while worked out how to add new ones?

(...we're allowed to talk about it now, right?)

Cocoa Solutions


Solution 1 - Cocoa

Yes, it's quite easy - you just highlight text you want to use as a snippet and drag it into the snippets area (dragging selected text can be difficult, I find it works better if you drag from the left edge of the editor). It brings up a dialog box asking you to name it.

Also, you can have custom parameters in snippets - if you put the text <#paramName#> anywhere in the code you are dragging, when you use the snippet it will come up as a replaceable parameter that you can tab between just like in the official Snippets.

Also make sure to set a completion prefix, which makes it shorter to activate the snippet as you are typing. The nice thing about using Snippets over other solutions like global system text expanders, is that snippets can also be limited to being valid in a certain area of code - so for example a snippet that filled out a string formatting line can be marked as valid only within a block, not outside a method. That way the completion prefix only hits in areas where it's valid to use.

You can edit a snippet by clicking once on it, a box will come up with the snippet text and some other snippet settings you can edit.

Solution 2 - Cocoa

A tip to help you drag the code every time, select text then click + hold (do not move mouse) until mouse pointer changes from "I" to pointer. You'll then be able to drag the selected text to the Code Snippet Library.

Solution 3 - Cocoa

Drag the highlighted code to the Snippet library. (It can be stubborn and not want to drag. Holding the mouse button down for a moment before dragging seems to help.)

Solution 4 - Cocoa

You can use Snippets to manage code snippets in Xcode. Check out this demo http://www.youtube.com/watch?v=il4kE4diy0k

Solution 5 - Cocoa

Simply select the code, the press ALT key and drag it into the code snippets library and rename it there, Make sure to press the ALT key, other than this you won'n be able to drag it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris DevereuxView Question on Stackoverflow
Solution 1 - CocoaKendall Helmstetter GelnerView Answer on Stackoverflow
Solution 2 - CocoaImranView Answer on Stackoverflow
Solution 3 - CocoaHugoView Answer on Stackoverflow
Solution 4 - CocoaVladimir PrudnikovView Answer on Stackoverflow
Solution 5 - CocoaMoe9977View Answer on Stackoverflow