Nov 30, 2022


Making your life easier with Xcode
snippets
,
shortcuts
and
templates



Tips and tricks to make you a more productive
iOS developer — Part 1

Marko Benačić

Typing a lot of code and smashing that mechanical keyboard a bunch surely can be satisfying. But at the same time, we can all agree that writing boilerplate code takes up a big chunk of our precious time.

Since every self-respecting developer strives to be as efficient as possible, it is important to master the tools provided by Apple’s Xcode if you wish to take your productivity to the next level.

This article is part 1 of a three-part series about Xcode’s snippets, shortcuts, and templates.

Part 2 — Xcode Shortcuts and part 3 — Xcode Templates are coming soon so make sure to come back and check on them!


#Snippets


Snippets are Xcode’s tool used for creating templates of frequently used boilerplate code. You have probably been using snippets without even knowing about them!

For example, if you start typing func, you will see a suggestion like the one in the picture ->




If you then proceed to hit enter, Xcode will generate a skeleton where you can easily input function name, parameters, return type, and a function body.

Once you’ve filled in a field (also known as placeholder token), you can press tab to switch to another one like so ->




#How to find snippets



You can find available snippets in 3 ways:

  • Using the keyboard shortcut command (⌘) + shift(⇧) + L
  • Using the bar menu on the top: View -> Show library
  • Pressing the + button at the top right






Open snippets button


After you’ve found the one you want to use, you can press enter or drag and drop it into your code:



There are many useful preset snippets already included such as docatch, guardlet, ifelse, available, etc.

Feel free to look around the library and try incorporating the ones you think would be the most useful to you!



#Adding your own snippet



Here comes the useful part. Let’s say you’re writing a lot of closures that are using references to self.

One way of avoiding retain cycles is by using [weak self].

If we then wish to use that self reference in our closure, we must first make sure the reference even exists
(that it is not nil).

One way of doing that is by using guard:


guard let self = self else { return }

Here’s an example:


Output:


Peanut Butter
Bananas


Since we are probably going to repeat those two little lines of code lots of times throughout our apps,
let’s create two snippets for them:


There are some other options to set for our snippet such as changing the preferred language (any language that is supported by Xcode), platform (iOS, macOS, tvOS, watchOS, or All), and scope availability, as well.

Feel free to test them out!

If we now proceed to write our shortcut (in this case “ws”), code completion will keep suggesting our snippet and pressing enter will write it out for us. Now we do the same procedure for the next line of code containing guard let self.

Using our newly created snippets should look something like this:



Wow! That was fast!!



#Placeholder tokens


I often use mark comments (and so should you!) to help me read through my code better.

To create a mark comment snippet with a placeholder token just type your placeholder name (eg. content) between <# and #> in your code, and create a snippet as shown above.






#Sharing your snippets


What if you want to share your super cool snippet with a friend?

All of your snippets are located at ~/Library/Developer/Xcode/UserData/CodeSnippets. They are saved as plist file format and you can copy and paste them directly into the CodeSnippets folder at the file path shown above.

Unfortunately, they are given a random UUID name, but you can simply open them with a text editor to find the one you want to share.



#That’s all…


Congrats! You’ve successfully created your first code snippet and made yourself a more efficient iOS developer!

In the next part of this mini-series, we will share more about using the best Xcode shortcuts that can help you become an even more efficient iOS developer.

If you liked this article or have any questions, feel free to contact us on LinkedIn.

We’re looking forward to meeting you!

next blog
back to all blogs