The Code Snippet Library is something many developers forget, but this is native in Xcode and can help you make your development faster.
There are certain lines that you code every day and more than once (e.g. comment blocks, custom logs, …), things that you most likely just copy over and over again, and then change some values in the code.
There is a perfect solution for this named code snippets!
Xcode is already predefined with a bunch of code snippets which you will find in the Code Snippet Library.
If you open Xcode and show the utilities, you will find the predefined code snippets on the bottom right.
It should look like this:
If you want to add a custom code snippet, all you have to do is write some code, no matter what you write, and drag it to the list of code snippets.
Let’s add an NSLog code snippet to use with a single string parameter.
Write the following code:
Thenotation is used whenever you want to specify that the user has to fill in a value. You can name it as you want.
Now drag it to the code snippet list, and the following window will appear:
You can name your code snippet by giving it a title.
This title will be used in the code snippets window.
For this example I used Single Line NSLog. You can give your code snippet a summary to provide a description to the user, you can also leave this empty if the title explains itself.
I will leave the title empty for the current example.
Select the platform on which you want the code snippet to be available.
You can, for example, only have your code snippet available for iOS and not use it in OS X.
This will make sure that your code snippets do not get mixed up, and you always have the code snippets available for your current development platform.
Select a programming language for your code snippet.
Next to the platform, you also have the option to select the programming language where you want this code snippet to be available in.
Because Xcode supports a bunch of programming languages, you can specify for which available programming languages the code snippets should be created.
For this example we will use Objective-C.
Set the completion shortcut.
After all info is filled in, give your code snippet a completion shortcut, like for example “NSSingleLog”, so that it will show up when typing for auto-completion in Xcode.
After this choose where you want the code snippet to available. “Completion scopes”
This option can be usefull for a lot of things. Maybe you want NSSingleLog snippet to be available through autocompletion in a method scope, but you may prefer to disable the snippet/autocompletion for example in header files.
With the completion scope option you have the possibility to tighten down the use of a code snippet.
After this is done, you just press “Done” and you have successfully created your first code snippet. It should look like this:
There are a lot of useful examples for using code snippets. For example typing properties and synthesizes.
@synthesize = _;
For the synthesize code snippet, I use the title "Synthesize Property", the completions shortcut "sss" and the completion scope "Class Implementation".
This means we can use this code snippet underneath the @implementation of your class. Not in the header file.
@property (nonatomic, retain) *;
For the property, I use the title "Retain Property", the completion shortcut "rrr" and the completion scope "All".
Note if you put this in the header file and drag and drop it to code snippets, the code language will be Objective-C++ by default and it will choose Class Interface Methods as completion scope.
You should change the completion scope to "All" if you want to use it in your private interface in the implementation file!
You should be able to build your own code snippets now! There are a lot of built-in code snippets available so be sure to check them out too!