By default, Apple provides some templates you can select when creating a new file. Xcode also provides a way to add your own templates, which can be a great way to speed up your development.

A custom Xcode template can contain just one file, but can also contain a group of files. A group of files can be great if you want to make a template for an architecture style, like the Clean Swift architecture. When you created a template for this architecture, all files that are needed for the setup will be generated automatically. Xcode templates support Swift files but also .xib files.

Select a template in Xcode when you want to add a new file

Default Swift template

Before we start creating our own Xcode template, we will first take a look at the default template that Apple provides for an empty Swift file.

Swift file:

//
//  ___FILENAME___
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  ___COPYRIGHT___
//

import Foundation

The template contains placeholders which are identified by the three underscores at the beginning and end of the property.

The following properties are available by default:

  • FILENAME – Name of the file including the file extension.
  • PROJECTNAME – Name of the project
  • FULLUSERNAME – Your full name
  • DATE – Current date
  • COPYRIGHT – Copyright information of the file

Create a custom template

Custom templates can be added to the following path on your Mac:
~/Library/Developer/Xcode/Templates.

In this folder, create a new folder named ‘File Templates‘. Inside this map, you can add your own templates. Templates in Xcode are sorted by category, like Source and User Interface. To add a template to a specific category, simply create a folder with the name of that category. If you create a folder with a category that currently doesn’t exist by default (like Clean Swift in the screenshot below), the category will be added in Xcode.

Folder structure for Xcode templates

Inside the category folder, create a new folder. The name of the folder is the name of the template in Xcode. The folder must contain the .xctemplate file extension so Xcode can understand that the folder is a template.

Inside the template folder, there are three files that must be present:

  • TemplateIcon.png – Icon of the template
  • [email protected] – Icon of the template, 2x for retina screens
  • ___FILEBASENAME___.swift – Contains metadata about your template

All other files that are inside the template folder, are files that will be added to your project when you select the template in Xcode. If you have multiple Swift files in the folder, all files will be added.

Xcode template folder
Example of an Xcode template with the files the folder contains.

An example of a template in the screenshot above can be downloaded here.

Creating the first template file

To keep it simple, the first custom Xcode template we will create is a custom UIViewController that automatically inherits an UIViewController. To start, you need to create a new Swift file inside the template folder with the name ___FILEBASENAME___.swift. Using the property ___FILENAME__, the filename will be the name that is provided when creating the file in Xcode.

Inside the file, add the following code:

//
//  ___FILENAME___
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  ___COPYRIGHT___
//

import Foundation
import UIKit

final class ___FILEBASENAMEASIDENTIFIER___: UIViewController {
    
}

The file is a bit different than the default Swift file provided by Apple. One of the different things is that UIKit is imported. Also, a new class is created which will directly inherit from UIViewController. The following properties are new:

  • FILEBASENAMEASIDENTIFIER – Name of the file excluding the file extension

Conclusion

Xcode templates are a great way to speed up your development work. After creating some templates for files you often create manually, you will be rewarded with more time that you can use for making awesome features in your app!

To help you build your first template, I will share some of my templates that can be useful when creating your template.

🚀 Like this article? Follow me on Twitter for more Swift related news!

Categories:Swift