How to Make a Code Snippet in Xcode
Tags: swift, ios, xcode
Published: 2021-01-29 23:44:33

---
title: How to Make a Code Snippet in Xcode
published: true
description: 
tags: #swift #ios #xcode
//cover_image: https://direct_url_to_image.jpg
---

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>DispatchQueue.main.async {
                    
                }</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If we highlight the code and then right click (⌘ + click) you will see an option to "Create Code Snippet".</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":1468,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://swifttom.com/wp-content/uploads/2021/01/Screen-Shot-2021-01-29-at-5.53.45-PM.png" alt="" class="wp-image-1468"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Once we click on "Create Code Snippet" a window will popup and prompt us to customize our code snippet.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":1470,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://swifttom.com/wp-content/uploads/2021/01/Screen-Shot-2021-01-29-at-5.55.10-PM.png" alt="" class="wp-image-1470"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>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". </p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":1472,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://swifttom.com/wp-content/uploads/2021/01/Screen-Shot-2021-01-29-at-5.59.41-PM.png" alt="" class="wp-image-1472"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:group -->
<div class="wp-block-group"><div class="wp-block-group__inner-container"><!-- wp:image {"align":"center","id":1477,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://swifttom.com/wp-content/uploads/2021/01/Screen-Shot-2021-01-29-at-6.05.13-PM-1.png" alt="" class="wp-image-1477"/></figure></div>
<!-- /wp:image -->

<!-- wp:image {"align":"center","id":1479,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://swifttom.com/wp-content/uploads/2021/01/Screen-Shot-2021-01-29-at-6.05.30-PM-1.png" alt="" class="wp-image-1479"/></figure></div>



<p>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! </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Thanks for reading and I hope this helps your coding productivity.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Happy coding!</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://64.media.tumblr.com/26460fbabb524c6906212d60cb0bfe2e/tumblr_inline_pae21mGEP91s6q7r3_500.gifv" alt="Texas Tribune Nerds - What I Learned at The Texas Tribune"/></figure></div>
<!-- /wp:image -->