5

If you show the Utilities pane on the far right of the XCode window, then select the "{ }" button, it shows code snippets that you can type the shortcut for, or drag and drop into your source code. How do you create your own?

Jay Imerman
  • 4,475
  • 6
  • 40
  • 55

2 Answers2

26

Although the documentation is very scant on this, I did find a blog post by Jason Brennan that describes step-by-step.

  1. Type in the code you want to create a snippet for.
  2. Highlight the code.
  3. Drag the highlighted code and drop it on the Snippet utility section.
  4. Click the snippet, a popup caption dialog will appear. Edit the title, description, and shortcut.

NOTE: You can use prompters denoted by <#prompterName#> that will give blue bubbles that you can tab to, and replace with "parameters" in your source. For example:

NSDictionary *<#varibleName#> = [[NSBundle mainBundle] infoDictionary];

NOTE: Depending on the language of the source code file you are editing, your new snippet will default to be in that language. In this way, snippets are selectively used for Swift or Objective-C depending on your context.

NOTE: If you attempt to drag highlighted text, and it unselects and starts a new selection instead, try to start your drag action in the whitespace off to the right of your selected text.

Also, to delete your snippets, click to highlight in the snippet library, and press the "Delete" key.

Jay Imerman
  • 4,475
  • 6
  • 40
  • 55
  • 1
    It's worth noting that if you're using a magic mouse you need to "right click" and drag. – Andrew Kozlik Oct 22 '12 at 19:47
  • As I am planning on buying a magic mouse next year and using that, I REALLY appreciate the heads up! – Jay Imerman Nov 14 '12 at 01:05
  • @AndrewKozlik No need to right-click on a magic mouse for me. – Nikolai Ruhe Jan 15 '13 at 11:30
  • I have a normal mouse, and I can't drag selected code, not even with modifier keys. I'm using Xcode 4.6. Any other way? – abc123 Mar 25 '13 at 23:38
  • @SKG, I found the dragging to be somewhat flakey. Sometimes it unhighlights, so be patient and try again. Perhaps it works better if you start dragging at the top-left of the selection? It's spotty. – Jay Imerman Apr 14 '13 at 18:22
  • Hold Option key and drag code to Snippets section. No need magic mouse – huynguyen Oct 12 '14 at 16:19
  • I found perfect solution. Add to your code for example NSLog(<#NSString *format, ...#>) and when you will write this you will have blue oval and after highlight all text what you need and drag with this NSlog - Profit! – Genevios Mar 13 '15 at 15:54
  • In Xcode 7 I noticed that when I click to drag highlighted text, instead of dragging it, it unselects and starts a new selection. To work around this, if you start dragging in the whitespace off to the right within the selection (past the end of the line), it will allows you to drag from the text editor. The latest beta seems to have fixed it, but that may give you a tip to avoid frustration later. – Jay Imerman Jul 24 '15 at 17:58
  • Sorry, found a BETTER answer. Click and HOLD on the selected text, until the cursor changes to an arrow, then you can drag and drop. – Jay Imerman Oct 07 '15 at 19:28
  • In Xcode 8 a long click to the right allows you to drag to the snippet library. – Jeff Dec 28 '16 at 18:11
4

Just highlight your snippet and drag it into the snippets box.

FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82
  • Thanks! I found the answer on another web page, wanted to post the q & a here in StackOverflow since it is such a great resource. (BTW, if the beer is free it probably isn't worth it!) – Jay Imerman Jul 22 '11 at 01:07
  • @Jay: Haha. Yeah, Apple provided free beer at WWDC the night of the party and most of it wasn't worth it =( – FreeAsInBeer Jul 22 '11 at 01:09