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?

3
  • would recommend using google prior to posting Commented May 22, 2013 at 15:02
  • Max, my point here was to log the Q&A in Stack Overflow, since that is a much better reference than Google for technical things. Thanks for reading and commenting! Commented May 23, 2013 at 20:46
  • You can use Snippets snippets.me. Check out this demo youtube.com/watch?v=il4kE4diy0k Commented Jun 25, 2013 at 18:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

It's worth noting that if you're using a magic mouse you need to "right click" and drag.
As I am planning on buying a magic mouse next year and using that, I REALLY appreciate the heads up!
@AndrewKozlik No need to right-click on a magic mouse for me.
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?
@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.
|
4

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

2 Comments

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: Haha. Yeah, Apple provided free beer at WWDC the night of the party and most of it wasn't worth it =(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.