What are code snippets?
Code snippets are as the name suggests, short pieces of code that can quickly be inserted into your code file. This is done either by dragging the snippet or by typing out the completion. Code snippets are very easy to create and use and can be applied in a wide variety of situations.
We will look at how you can create & use snippets. The following example is done in a playground, but this could be done from anywhere within Xcode.
Note: The example below was performed on Xcode 11.7
How do we create code snippets?
- Start off by writing the code or text that you want to convert into a snippet. For example, I have a set of comments that I add at the start of every function. Write it down.
/** This function performs a comparison of the 2 objects - important: This function does not perform data validation. - returns: `Bool`. - requires: iOS 13 or later - Since: iOS 13 - parameter lhsValue: This holds the value on the lhs of the operator - parameter rhsValue: This holds the value on the rhs of the operator - Example: `var answer = venueAddress == hotelAddress` - author: Arun Patwardhan - copyright: Copyright (c) Amaranthine 2020 - date: 14th September 2020 - version: 1.0 */
2. Select it.
3. From the menu bar select Editor > Create Code Snippet.
This brings up the snippet editor.
4. Give your snippet the following details.
|Name||This is the name of your code snippet.|
|Platform||This determines whether your snippet is available only for certain platforms: say only for iOS.|
|Availability||This determines the place where the snippet can be added.|
|Completion||This is the word that we will be typing in the Xcode editor to trigger the implementation of the snippet|
|Language||This specifies the language for which the snippet will be applied.|
Name: Func Documentation
Availability: All scopes
Note that the values for Name and Completion can be whatever you want.
This is how the snippet should look.
5. Now we will try to use it in the editor. Start typing the completion word in the Xcode editor.
6. Select the snippet with your name and completion.
7. Hit enter. You should see the comments you want appearing in the editor.
We can make our snippet above even better by using placeholders. Placeholders are pieces of text that can be replaced by the user. They also give information about what is expected in the placeholder.
We can add place holders by simply typing the hint inside placeholder brackets. Placeholder brackets are nothing but open <# and closing #>. For example:
<# some text #>
Which appears as
There are plenty of places in our comments where we can use placeholders. When we use the code snippet it should put comments with place holders in them.
- Let us change the comments in our Xcode editor first. We will edit the snippet later on. Make the changes as shown below.
/** <# put the description of your function here #> - important: <# mention some important points here #> - returns: `<# return type #>`. - requires: iOS <#iOS Version#> or later - Since: iOS <#iOS Version#> - parameter <#param 1#>: This holds the value on the lhs of the operator - parameter <#param2#>: This holds the value on the rhs of the operator - Example: `<#put some example code here#>` - author: Arun Patwardhan - copyright: Copyright (c) Amaranthine 2020 - date: <#day#> <#month#> <#year#> - version: 1.0 */
We have made the following items into comments.
- OS Version
- Return type
- Important comments
- Parameter 1 & 2 names
- Sample code
- Day, Month, & Year
Of course, there are other things we could change too. Feel free to make any other changes you can think of.
2. Let us now copy these changes to the code snippet we created. Copy the code from the Xcode editor.
To bring the snippet editor again simply click on the add object button in the upper right hand corner of Xcode.
4. Select the snippet from the list on the left and click edit.
5. Paste the code that you just copied. Your snippet editor should look like this:
6. Click on ‘Done’ once you are finished making changes. Your snippet will now be ready.
7. Try adding the snippet into your editor just like before. Simply type in the completion for your snippet.
We can use the autocompletion we saw earlier. But it is also possible for us to drag snippets.
Exporting code snippets
Once created it is possible to export/import code snippets too. All the snippets are located in the following folder.
Any snippets you have created will be located there.
Any new snippets to be added will have to be added there.
Code snippets are easy to create and have several advantages:
- They improve the developers experience
- Promote consistent code
- Speeds up the process of writing code
- Encourages developers to use each others snippets and gain the first 3 advantages.
Creating and using snippets is very very easy and has a lot of benefits. So go ahead and create snippets.
One thought on “Creating Code Snippets in Xcode”
Pingback: Creating custom operators in Swift | Arun Patwardhan's Blog