Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add my own code snippets to xcode 4?

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

like image 305
Chris Devereux Avatar asked Mar 14 '11 19:03

Chris Devereux


People also ask

How do I add a code snippet in Xcode 12?

Creating a code snippet is easy. Just select the code that you want to make a snippet and drag it to the code snippet section towards the bottom right of the xcode. A window will appear and there you can rename it and add a description.

How do I import code snippets?

Import a code snippet You can import a snippet to your Visual Studio installation by using the Code Snippets Manager. Open it by choosing Tools > Code Snippets Manager. Click the Import button. Go to the location where you saved the code snippet in the previous procedure, select it, and click Open.

Where can I store my code snippets?

There are various ways you can store a code snippet, either by keeping it in a GitHub repository or can use a smart code snippet manager, like Codiga's Coding Assistant. Coding Assistant allows you to store and reuse code snippets.

How do you make your own snippets?

Create your own snippets# To create or edit your own snippets, select User Snippets under File > Preferences (Code > Preferences on macOS), and then select the language (by language identifier) for which the snippets should appear, or the New Global Snippets file option if they should appear for all languages.


2 Answers

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.

like image 117
Kendall Helmstetter Gelner Avatar answered Sep 28 '22 09:09

Kendall Helmstetter Gelner


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.

like image 22
Imran Avatar answered Sep 28 '22 08:09

Imran