What is the first thing you do when you are working on a new project/adding new features or even refactoring your code? Yes, it’s CREATING NEW FILES.

We all know the usefulness of the following screen and how it is an integral part of our coding time but just take a minute to think of the number of clicks you do to create files for architectures such as MVVM, VIPER, VIP, etc.

Fig 1.1

Imagine having a tailor made template that just creates all the files you want in a single click. In addition, could also have some boilerplate code that suits your’s/ company’s coding style.

In this tutorial, I will walk you through preparing a custom template for a simple VIP project architecture which allows users to make choices depending on his needs.

Step 1: Understanding the basics of Xcode templates

You can find all the Xcode default templates in the dedicated folder here: /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates

Here, you can see all the folders that you see as group name when creating new files[refer Fig 1.1].

If you go inside Source, we can open the subfolder named Swift File.xctemplate and see a group of files:

There is a file with extension .swift, then we have two image and finally a .plist file. Let’s look at in detail:

**1.FILEBASENAME.swift: **This file is the one which is created when the user hits the create button. So, when a user creates a new file, Xcode creates a copy of the FILEBASENAME.swift and all instances of the FILEBASENAME macro are replaced by the name provided by the user in the dialog. If you open the file, you will see some boilerplate code. In this case: import_ Foundation_

**2. templateInfo.plist: **This file contains information about the template. It contains additional metadata about your template, such as description, summary, kind, options, etc.

3. templateIcon.png

This icon will be used in the template selector.

4. templateIcon@2x.png

Same as TemplateIcon.png but is used on Retina Mac’s.

Step 3: Build new custom templates

The easy approach would be to simply create a new group folder and copy one of the new subfolder in the **/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates **directory, however doing so has a problem. If Xcode is updated, then the File templates folder is deleted and all our templates are lost. The best way is to create a specific folder here:

~/Library/Developer/Xcode/Templates/

Start with creating a folder File Templates and subfolder Xcode templates. Underneath the Xcode templates, create a VIPArchitecture.xctemplatefolder.

Additionally, you can use the script DownloadCustomTemplates.sh to create this for you. You can find it here

I added to the VIPArchitecture.xctemplate folder a set of icons:

TemplateIcon@2x.png — 96 x 96 Icon

TemplateIcon.png — 48 x 48 icon

templateInfo.plist file with this content:

and two subfolders Default and WithWorker.

without worker

With worker

#ios-development #ios

Add Custom Xcode templates
1.20 GEEK