Xcode code snippet

Balraj Verma
5 min readSep 23, 2023

--

Streamline Your code with Xcode Code Snippet Library

Image by WangXiNa on Freepik

Have you thought about how wonderful or beautiful it would be if a piece of code you wrote is immediately accessible to everyone on your team in their Xcode code snippet library? I am assuming you have utilized a few pieces of Xcode’s code completion, which is another illustration of it. Today, we will discover how to include our own generic code in the Xcode code snippet library. Lets start. 🏇🏼

Code snippet is a reusable piece of code or code template that you can save and quickly insert into your source code. Code snippets are designed to save you time by providing a way to easily include commonly used code patterns, structures, or even just pieces of code that you find yourself typing frequently. Its a very good feature provided by Xcode, Let’s see how you can create your own code snippet available in code snippet library.

You can launch code snippet library using the (+ ) button on Xcode tool bar or you can simply press ⌘(command) + ⇧(shift) + l(L). Or you just right click any where in your source file and select option create code snippet….

launch code snippet library
Another way of launching code snippet library.

Once it is launched, a window like to below one will appear.

Xcode Code Snippet library

You may say that all of the code snippets you can see on the left side of code snippet window are built into Xcode. There are also code snippets for other languages; they are not just for Swift.

After you have finished writing the code you wish to include in the Xcode snippet library, right-click on it to add it to the library. When you tap it, a window will appear with an option to create a code snippet. For instance, I developed the code snippet for “userdefault” (I believe it is fairly typical code that every developer may write to obtain certain values from a key; why can not we add it to the code snippet library 🧐)

To the code of your code snippets, you can add Title. Add a description as well. Now here you have more flexibility to write, you can include comments, explanations, and even examples of your own code; nevertheless, being more specific will be beneficial. Additionally, there are a few more configuration choices below.

Language: The language supported by your code snippet

Platform: You can add it to whichever platform you choose, such as iOS alone or all.

Completion: This feature, which is highly significant, allows you to add your own keywords that the compiler will use to prompt to accomplish your code automatically. Try to make your name short so that the compiler’s auto-completion prompt will not require you to type the entire term.

Availability: This feature helps ensure that code snippets are only available and applicable in specific contexts, such as certain code files, functions, or code blocks.

Therefore, our code snippet is now ready for use. I have written an example of code below for your understanding.

Sample code snippet

I added availability for all scopes and the completion to getVal to the code sample above so that every time I write getVal, the compiler will give options for autocompletion.

Use custom code snippet added in library.

Additionally, you can provide a placeholder in your code for that option if you have generated a code in which you want the user to enter their preferred name or any other info. i.e var name etc, you can use <# keyword #> for your placeholder input from user. It can represent what ever value you will mention between hashes i.e #Type#, #default value#, #description#, #expression#>, #function# etc,

Your code snippet is now complete, and you can find it in the given path below. There, you can see the code snippet you recently made with .codesnippet file extension.(It just an xml, you can review its content with some text editor) Making it accessible to every team member is the last step. To make it accessible to everyone, you have to publish it to a team repository. The file will then be downloaded by your team members and saved to the codeSnippets directory on their Xcode. After that, users must restart Xcode in order to see the modification reflected in their Code snippet library. The location where each team member will add the code snippet file or, find the existing one, is listed below.

~/Library/Developer/Xcode/UserData/CodeSnippets/

Additionally, it is advised to use code snippets if you just need to use a small piece of code. if you are creating larger code files that are linked to one another classes internally. Then, for such code autocompletion, you need utilize the Xcode File Template. (We shall discuss it soon)

You can contribute your code to the snippet library in this manner.

All done. I am glad that you took the time to read this article, and I thank you. If you found it useful, clap or leave a comment And we will meet in next one 🤝

--

--