How to add a custom code snippet in Xcode

Code Snippets is a useful and valuable feature in Xcode(IDE). Developers who use Xcode for their App or any other project development can use code snippets. It saves a lot of time. Instead of duplicating codes or rewriting, we can use code snippet. It also helps a lot in avoiding the mistakes in a code.

How To Add a Custom code Snippet?

In Xcode, You can add your own code snippets. For adding the custom code snippets, just drag and drop the selected code into code snippet library.

Code_snippet_1_of_2_2x

Once the code is dropped in the snippet library, it will prompt a window for adding a title, shortcut, language, and summary. Also if you want to edit the custom code snippet, you can do it.

Code_snippet_2_of_2_2x

How to identify custom code snippets in snippets library?

In the code snippet library, you can see “User” text in the bottom right corner of the open&close flower braces icon.

Custom Parameter in Code Snippet:

You can also have custom parameters in the code snippet. In the code, if you put the text as <#paramName#>, then the code will ask for the parameter. There you can add our parameter based on the code.

Title:

Name of the code snippet. The title is used to identify the code snippet.

Summary:

You can specify what this code snippet will do. You can give your own comments for the explanation.

What is Shortcut in code snippet?

You can add a shortcut in the code snippet. If you type the shortcut text in the code editor, then the auto-completion code will pop up. By using this you can save time.

What is language in code snippet?

When inserting a new code snippet, you can specify for what languages this snippet should show. If you specify the language as swift, then the Xcode will automatically detect the file editor type and show the corresponding code snippets in the snippets library.

How to edit a code snippet?

By clicking the code snippet,  a pop-up window appears. And in the pop-up window, there is an edit button, in the bottom left corner, by selecting the edit button you can edit the code snippet in the library.

Note: You can’t edit the default code snippets in the library.

Being an iOS developer Code snippets helped me to a greater extent in making my development processes efficient. Code snippets made the process easier, cleaner and simpler.

Yogesh,
iOS Team,
Mallow Technologies.

Leave a Reply

%d bloggers like this: