Save time by adding custom Xcode templates

Speed up the development time by automating creations of files.| VIP architecture

Parul Vats
Level Up Coding

--

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.xctemplate folder.

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

a templateInfo.plist file with this content:

and two subfolders Default and WithWorker.

without worker
With worker

The options field in the templateInfo.plist defines which folder will be picked by Xcode to generate files. So when you choose to create a new file and select the custom template, you would be presented by the following screen:

Now if you know VIP architecture, for each module you need to create a worker, presenter, controller, and interactor. For some modules, we don’t need worker and it might just be an overhead to create one. This custom template provides you with an option to skip creating a worker if you don’t need one. So, with just one click, it creates for you following files:

These files contain some boilerplate code which speeds up the development time if you need to write repetitive stuff each time.

The above file shows the common boilerplate code that I wrote for each module when using VIP architecture.

With minimal effort, you can create your own custom templates or customize the content of your files.

References

https://medium.com/overapp-ios/create-xcode-templates-c968d4b43f7b

--

--