Creating your own templates in Xcode

Christoffer Winterkvist
itch design
Published in
4 min readApr 21, 2018

When developing applications, there are loads of tools and techniques that can help you save time and make you more efficient. One such tool is templates, little snippets that give you a more concrete starting point for what you want to achieve. Xcode comes with some built-in templates but templates truly shine when you tailor them to your code style, pattern and other personal preferences. Lucky for us, Xcode supports this, but getting it set up can sometimes feel like practicing voodoo magic as Apple neglect to promote this hidden gem, and we result to blog posts to figure it out.

Creating your templates folder

Xcode looks for templates inside ~/Library/Developer/Xcode/Templates.
This folder does not exist out of the box so you will have to create it manually.
You can do that in the terminal by running the following command:
mkdir ~/Library/Developer/Xcode/Templates
Or navigate to your Xcode folder in Finder using ⇧⌘G — “Go to folder” and paste ~/Library/Developer/Xcode/

Inside your newly created templates folder, you can create subfolders to categories your templates.
At the time of writing, I went with one folder called Custom.

A good starting point for creating templates is to duplicate one of the existing ones. I’ve made my templates based on the Swift File.xctemplate

You can find it here:

/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File\ Templates/Source/Swift\ File.xctemplate

Creating your template

Xcode templates have their own file type called .xctemplate.
It works as a container for all your template related files.
A basic Swift template consists of four files.

  • TemplateIcon.png
    This icon will be used in the template selector.
  • TemplateIcon@2x.png
    Same as TemplateIcon.png but is used on Retina Mac’s.
  • TemplateInfo.plist
    The property list holds additional metadata about your template, such as description, summary, kind, etc.
  • __FILEBASENAME__.swift
    This is the template file that you will customize to fit your needs.

The __FILEBASENAME__ placeholder will be replaced with whatever you input in the template picker; this can also be used inside your template.

Here is what a simple UIView template could look like:

Snippets in templates

One thing worth mentioning is that you can use templates in combination with snippet placeholders. If you are familiar with the concept of Xcode snippets, you’ll know that for a specific snippet you can add placeholder tags that the user can navigate between and replace with the actual code. This helps speed up the process and makes your templates easier to work with.
You can add snippet placeholders to your templates by inserting them as <#MyPlaceholder#>. This will then be replaced with a macOS token element.

Examples

To share some inspiration, here are a few of my templates that I use on a regular basis.

Flow controllers

My flow controllers usually consist of more than one controller. Personally, I tend to “default” into making them a FamilyViewController.

UICollectionViewCell

This is not a very advanced template but seeing as I do all my UI components programmatically without interface builder, I always override the initializer and add my custom views there. This forces me to add the required initialize with coder function; the templates saves me the time it takes for the compiler to see that the method is missing and me having to invoke Fix all issues. It is not much, but everything counts in the long run.

UIViewController

This template simply implements the viewDidLoad method as this is where I tend to add the most of the setup for my view controllers and initializers are hard to make generic, especially if you rely on dependency injection.

As you can see from all my examples, my templates are very lean, some of them do little to nothing except taking the ___FILEBASENAME___ and use it as the class name. The reason for this is that I want them to be neat and tidy. I feel that if I make templates and need to remove a lot of things from them each time I’m about to use them, then they don’t serve their intended purpose. Too advanced templates are also more demanding to work with from a creative point of view, as it will require you to make decisions & think things through before creating your canvas, sometimes without a relevant context. I like my canvas to be free of clutter which makes them more inviting.

When you start using templates, you’ll quickly find that it can be quite challenging to find a good middle-ground between what is semi-niche and what is perfectly generic. But remember, there are no right and wrong with templates, they should be personal, create templates that fit your needs and make sense in your context. If they don’t work out, just change them until they do.

If you have any good tips that you’d like to share, just hit me up on Twitter and we can compare notes. You can reach me at @zenangst.

--

--

Christoffer Winterkvist
itch design

random hero at @fink-oslo by day, cocoa vigilante by night, dad at dawn. my life is awesome. Previously @hyperoslo