66

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?)

asked Mar 14, 2011 at 19:09

5 Answers 5

162

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.

answered Mar 14, 2011 at 19:17
5
  • 13
    I find if you hold down the Option and use the crosshair cursor it makes it much easier to drag the selected text. Commented Mar 25, 2011 at 18:03
  • 29
    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. Commented Aug 15, 2011 at 0:22
  • +1 But I have found Snippets so difficult to use, I just started using TextExpander, which I already use in other IDEs. Global Snippets, if you will. Commented Sep 2, 2011 at 18:11
  • With the completion prefix I still prefer Snippets, but I can understand wanting a global solution for text fragments... Commented Sep 3, 2011 at 9:12
  • 2
    Highlight the snippet text and put the cursor on <#paramName#> then drag. Commented Jul 19, 2013 at 7:54
31

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.

Kev
120k53 gold badges307 silver badges396 bronze badges
answered Aug 15, 2011 at 0:22
1
  • 2
    This is literally the only way I can get it to work, and I would have never thought of it. Thanks a bunch! Commented May 10, 2012 at 16:42
6

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.)

answered Dec 20, 2012 at 3:54
1
  • This works. Hold the mouse button down for about two seconds. The cursor will change and then you can move the snippet. Commented Oct 19, 2016 at 17:59
1

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

answered Jun 25, 2013 at 18:41
-1

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.

answered Jul 6, 2013 at 14:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.