How to Create Code Snippets in Xcode 11

Hello everyone 👋 Today I am going to teach you how to make code snippets in Xcode 11, in 5 simple steps! Knowing how to make a code snippet will make future projects so much more simple, and that’s what we live for...things being simple 😉 Let’s get started.

CodeSnippetsScreenshot1.png

I have a function here called “showAlertController.” I want to make this into a code snippet so I can use it in future projects.

 
CodeSnippetsScreenshot3.png

Step 1:

We are going to make that happen by highlighting all of the code that we want included in the snippet, then right click (secondary click), and select the ”Create Code Snippet” option.

 
CodeSnippetsScreenshot5.png

S

tep 2:

Once you select “Create Code Snippet” you will see a menu pop up on your screen. This menu will have a “Title” section, which will be the name of the snippet. We’ll make the Title “Show Alert Controller.”

 
CodeSnippetsScreenshot6.png

Step 3:

You will also notice the menu has a “Summary” section, which is a place where you can put a short description about your code snippet. We’ll make the Summary, “Returns a UIAlertController based off of the given title, and body text.” This is what best describes our code snippet, and it’s function.

 
CodeSnippetsScreenshot7.png

Step 4:

Next, will be the “Completion” section, AKA the “Completion Shortcut”. You will see other options here such as “Language, Platform, and Availability” but we are going to leave all of these at their defaults. However, whatever you name your “Completion Shortcut” is what you will have to type in your project to get the code snippet to show up. We’ll name the Completion Shortcut “salert.”

 
CodeSnippetsScreenshot8.png

Step 5:

Last but not least, click the “Done” button on your menu, which will take you back to Xcode. Now if you type the name of your Completion Shortcut into Xcode, which was “salert”, and press enter it will autocomplete your code, and fill the page with your code snippet!

 

And that’s all! 🙌

That’s how to make a code snippet in 5 simple steps.

I hope this helps all of you, and makes your future coding projects more simple 🍎

Don’t forget to let us know what you think, because we love hearing from our iOS Made Simple family! If you guys love this content and want more, don’t forget to subscribe to our weekly newsletter.