Create your own Xcode templates

Xcode 8, macOS 10.11

(I will test this with 10.12. Settings mostly borrowed from Credit for working this out mainly goes to them, but I wanted to add a few additional observations. Also, I’m working on macOS rather than iOS.)

When you are using certain classes a lot in your projects, having them as templates would be nice, so you don’t always have to drag them in from another project (and maybe forget to set ‘copy if needed’ and find that they break months later or that you’ve suddenly edited something you never meant to edit and when you look at your old project again you find it changed and broken).

Here, I’m using a ColourView as an example (I’m programming in British English. Sue me.)

1) Set up the template folder
a) [~ refers to your username folder]: Navigate to ~/Library/Developer/Xcode/Templates
If the templates folder does not exist, create one. Many articles, including the one referred to above, will give ~/Library/Developer/Xcode/Templates/File Templates/Custom as the path – I’ve found both versions listed, and the shorter form above works for me; the file shows up in the customs section of the FileTemplates, as well as the FileTemplateLibrary in the Utilities bar
b) create a folder for your project, give it the template name, as well as an .xctemplate extension – here, I am using colourView.xctemplate
c) add a TemplateIcon.png file to the folder. This is the easiest part.

2) Add an empty Swift file to your template’s folder, and name it ___FILEBASENAME___.swift
This is a placeholder string which ensures that your new file works like any other Xcode template.

The full contents of mine are

— begin file

// ___FILENAME___
// Created by ___FULLUSERNAME___ on ___DATE___.
// Copyright ___YEAR___ ___ORGANIZATIONNAME___. All rights reserved.

import Cocoa


@IBInspectable var backgroundColor: NSColor = NSColor.white{
self.needsDisplay = true

override func draw(_ dirtyRect: NSRect) {

— end file

This is a subclass of NSView that has a backgroundColor property (which views on Mac do not, unlike iOS). It is not layer-backed, for complicated reasons, though you could make your default view layer-backed and then use the view’s property to set the layer’s background colour). I find that I want to use differently-coloured views A LOT in my coding, mainly to differentiate between items, and see where layouts go wrong.

3) Add a TemplateInfo.plist to your template’s folder. To work, this needs to have a number of pre-defined keys. (I have not found more info for this in Apple’s documentation. They’re pretty self-explanatory, but I have no idea whether there are any alternatives or additional tweaks you could use.)

Mine is

or, if you don’t want to squint and are looking for cut-and-paste code,

NSView subclass with background colour property
NSView subclass with background colour property
Platforms was a lucky guess; the key for the iPhone is

I would guess at watchOS and tvOS, but I cannot guarantee them, and again, I’m not finding the keys listed in a definite listing. In older articles (e.g., the build settings listing at , I have found ‘iphonesimulator macosx iphoneos‘ as possible values (well, you cannot create a template for the simulator anyway), but as I said, macos (not ‘macosx’ works with Xcode 8) and is consistent with Apple’s new naming scheme.

4) create a new file, scroll down the list of possible templates, and your brand new shiny colourView should turn up in the ‘custom’ section.