Creating custom code snippets with XCode 10 made even easier

Sagar More
3 min readOct 6, 2018

How to create custom code snippets with XCode 10

stocksnap.io

What is the need?

Have you come across the situation, where we write the same code again & again with little changes in its implementation? Probably yes, right!

We iOS developer in love with UITableView, we like to use it in almost all controller & this UITableView comes with a list of delegate & dataSource methods that need to implemented by the developer in every controller, only it’s implementation gets changed.

Don’t you think, there should be a mechanism, in which I just type one word & all UITableView delegate/dataSource methods get should appear on the editor?

Here, the Xcode custom Snippets come in. You just write your own custom snippet for UITableView delegate/dataSource and can use any time with a keyword associated with its snippet.

This is just one case, but you can write your own custom snippets for N number of code operations in Xcode.

How to create snippets?

Creating snippets is super easy with XCode 10. Go to Editor->Create Code Snippet. A window will appear, in which you can write it’s a definition.

Creating own custom code snippet

If you want to add a placeholder in snippet simply put it between <# and #> and it will be transformed in both editor and final snippet.

How to use snippets?

  • You can specify completion shortcut while creating custom code snippets. So when you type completion shortcut in editor area relevant snippet will appear on screen to choose from. Like in the below gif example, I created the completion shortcut with named snippet-uitableviewdelegate .
Completion shortcut for the custom code snippet
  • In Xcode 10 Code Snippet Library moved to the top toolbar area, as shown below GIF. Just click on it, list of code snippets will appear then choose your code snippet.
Use Snippet from Code Snippet Library

Bonus

Do you want already created some commonly used custom swift code snippets? Get it from my GitHub Repository.

If you liked this post, please share & give some clapps 👏 !

You can follow me on Medium for fresh articles. Also, connect with me on LinkedIn.

happyCoding()-> 😃

--

--

Sagar More

Sr. iOS Developer @ Delivery Hero | Mobile Enthusiast | Swift Expert