Xcode code snippets

Snippets, one step closer to Flash



Sloths are cute, but not easy to watch them climbing on a tree. It seems decade and yet, they just moves some inches. How about sloth speed coding! Ever think about it? And what impact does it have on a pair programming session. I know I know it is very horrible and frustrating both to watch and to be the sloth speed coder. Why not we change that. The very first step will be to become fast on our repetitive coding. There are many times when we write down a chunk of code times after times. Didn’t think about storing that reusable code chunk on Xcode and then call them later on by just two or three keystroke. The reusable code is generally termed as Snippets, more specifically Code Snippets. This blog post is dedicated on this Xcode Code Snippets.

Xcode does provide ways for creating, editing and deleting Code Snippets. But it an ever changing word of iOS development. With each new version of iOS there is the new Xcode and also things start to change places. And again things are placed and behave different on a Xcode Project and on a Xcode Playground. As a result it becomes important for us to know the hows of Code Snippets on Xcode and where the Code Snippets will be on a project and on a playground.

Snippets does two things awesomely. Firstly it saves a log of time and secondly lessen the monologues felling of writing repetitive code.

The time we will spend for making those snippets will eventually pay a lot on the near future.

Small or big, doesn’t matter it will pay us good. So let’s not be some junky. Let’s make snippets.

This blog post is based on Xcode 10.1.

Background

We also have a blog post about Xcode & Simulator shortcuts. The current and also the shortcuts blogpost has the same goal. To make us a better road runner. You will find the shortcuts blog post very interesting.

Creating Placeholder

We would need to have a placeholder marker, which will be automatically selected when we press tab on the keyboard. Place the desire text, in our case it is Placeholder, in mid of two hash(#) embedded with angle brackets(< >) to create the placeholder.

<#Placeholder#>
Creating Placeholder

Creating Snippets on Xcode Project

 Select the code and by pressing the left button of the mouse, you will see a menu with Create Snippets with this code. Press it and the snippet is added to the Snippet list. We can edit the options as we need.Pretty simple. But will not work on Playground. For playground follow the next step.

Creating Snippets on Xcode Project

Creating Snippets on Playground

Select the code and then go to Editor-> Create Code Snippet. And done. Simple right?

Creating Snippets on Playground

We only just created a code snippet. But to use it effectively we need to update the snippet as our need. Let us Edit the Snippet.

Editing Snippets

On the top right of Xcode press the code block sign,{}. List of code snippets will be visible. From there we can edit the Snippets as we need.

Editing Snippet

Snippets completion

If we already defined Completion Shortcut then we can just type that shortcut and will get the list of possible completion code block, among them select the desire snippets.

Xcode Snippets completion

Removing Snippets

To remove the Snippet bring the Snippet list by pressing the top right code block sign, {}. From the list selected the Snippet you want to remove. On the first selection/tap the details view of the Snippet will be visible. Tap or press the snippet on more time to dismiss the details view of the Snippet. Now press delete on keyboard. A confirmation alert will be visible, confirm to delete the Snippet.

Removing Snippet

Xcode Snippet Playlist

All the content of this blog post is available on youtube through a playlist.

Xcode Snippet Playlist

There are some options like Platform, Language, Completion Scope on the Snippet editing time. Those are good to limit the view scope of the Snippets. But by the time we try to limit the view scope of the Snippet we have to keep in mind that, the Snippet will have to work on Playground also. This is true for the Xcode 10.1, Hopefully will be fixed on some future version of Xcode.

If you haven’t already, I will suggest you to visit the Xcode & Simulator shortcut blog post at least once. I can guaranty the resources will be some very good help.

At the end we all know time is valuable. So we should concentrate on efficiency. Let us be an efficient developer, the very first step is to reduce the redundant work. Happy Snippet creation ⛏ 🔩🛠.

Resources

2 thoughts on “Snippets, one step closer to Flash

Leave a Reply

Notifications for mobidevtalk! Cool ;) :( not cool