How to Make a Code Snippet in Xcode

Code snippets are shortcuts to blocks of boilerplate code that we use most often when programming. Xcode allows us to add custom snippets to the many pre-existing snippets that Apple provides. Let’s see how we can make a code snippet out of the following code below.

DispatchQueue.main.async {

You might be familiar with the example code above. This block of code is used to make sure things like network calls are made on the main thread and not on a background thread. Let’s make a code snippet of this so we don’t have to type the whole thing out every time we want to use it.

If we highlight the code and then right click (⌘ + click) you will see an option to “Create Code Snippet”.

Once we click on “Create Code Snippet” a window will popup and prompt us to customize our code snippet.

First we will name our snippet “Dispatch Main Queue” (Feel free to name it whatever you want 😄). Now we will leave everything else the same except the “Completion” section. In the “Completion” textfield we will type in “dpm”.

What this does is every time we type “dpm” Xcode will prompt us in the autocompletion to see if we want to use our code snippet! So from now on anytime we want to put something on the main thread all we have to do is type “dpm” and we get our code snippet we just created.

Just like that we have made a code snippet and saved ourselves time by never having to type out all that code ever again! Feel free to make code snippets of any block of code your tired of typing out! Embrace your laziness!

Thanks for reading and I hope this helps your coding productivity.

Happy coding!

Texas Tribune Nerds - What I Learned at The Texas Tribune

Leave a Reply