Create your own XCode template

Very often you create a new class or a new view controller and customize it before start coding.

Every time you select from XCode, New File, the file type and add it to your project, like this:


Why you do not create a custom file template with customized options based on your exigence?

Like this:

This screenshot show you how to create a new file(s) with the type you prefer:

  • Class
  • Codable Struct
  • Enum
  • Extension
  • With Storyboard, with XIB or nothing
  • etc..

XCode original templates

If you want to take a look to the original XCode templates, that are installed in this folder:

/Applications/Xcode.app/Contents/Developer/Platforms/
  iPhoneOS.platform/Developer/Library/Xcode/Templates

Templates -> File Templates
          -> Project Templates

Creating a new template

Start creating a new folder wherever you want (is not important for now). Call it “Custom Swift.xctemplate“.

Custom Swift” will be the name of the file in the source group of New File, in XCode.

Next step, copy one existing “TemplateInfo.plist” from another template in your folder and open it with XCode for editing (or copy the following code and save as TemplateInfo.plist, suggested.)


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>DefaultCompletionName</key>
	<string>Custom Swift file</string>
	<key>Description</key>
	<string>This generates a new Swift file using custom format template.</string>
	<key>Kind</key>
	<string>Xcode.IDEKit.TextSubstitutionFileTemplateKind</string>
	<key>Options</key>
	<array>
		<dict>
			<key>Description</key>
			<string>The name of the module to create</string>
			<key>Default</key>
			<string></string>
			<key>Identifier</key>
			<string>moduleName</string>
			<key>Name</key>
			<string>New Module Name:</string>
			<key>Required</key>
			<string>YES</string>
			<key>Type</key>
			<string>text</string>
		</dict>
		<dict>
			<key>Default</key>
			<string>___VARIABLE_moduleName___</string>
			<key>Identifier</key>
			<string>productName</string>
			<key>Type</key>
			<string>static</string>
		</dict>
		<dict>
			<key>Identifier</key>
			<string>interface</string>
			<key>Name</key>
			<string>Generate Interface Builder:</string>
			<key>Description</key>
			<string>Which interface builder to generate</string>
			<key>Type</key>
			<string>popup</string>
			<key>Default</key>
			<string>None</string>
			<key>Values</key>
			<array>
				<string>XIB</string>
				<string>None</string>
				<string>Storyboard</string>
			</array>
		</dict>
		<dict>
			<key>Identifier</key>
			<string>complex</string>
			<key>Name</key>
			<string>Module type</string>
			<key>Description</key>
			<string>Type of a module to generate</string>
			<key>Type</key>
			<string>popup</string>
			<key>Default</key>
			<string>Struct</string>
			<key>Values</key>
			<array>
				<string>Class</string>
				<string>Struct</string>
				<string>Enum</string>
				<string>Extension</string>
			</array>
		</dict>
	</array>
	<key>Platforms</key>
	<array>
		<string>com.apple.platform.iphoneos</string>
	</array>
	<key>SortOrder</key>
	<string>1</string>
	<key>Summary</key>
	<string>This generates a new Swift file using custom format template.</string>
</dict>
</plist>
TemplateInfo.plist opened with XCode property file

Template composition

Step by step description of the template file and how it works:

HEADER

This information is shown in the source file. There is the name of the new file to create and the description.

OPTION 1

In the “Option” section you can decide the customizable options for your template. This information are shown after selecting your template, in the “next step” screen.

Here you can set the textbox value and description, aka the name of your module and a custom variable “moduleIdentifier” to use later.

OPTION 2

Here we can decide the moduleName to use and replace in all of your created files

OPTION 3

This option is used to show a “Combo box” with some options to select. In this case, we want to create a new Swift file + a XIB or Storyboard or Nothing.

Same as Option 1, you can set also the description of the field.

OPTION 4

Same as before, a new combo box to select the module type you want to create, the same fields for description of the field.


Adding files to template

Following the .plist file you need to create different folder structure and files that structured as the combo box items, for instance:

If you select “XIB” as interface and “Struct” as type, you need to create a new folder called “XIBStruct” and put inside your files. In this case, you should put inside a new XIB and a new Swift file.

Every file you create in that folder should be called “___FILEBASENAME___ViewController.xib” and “___FILEBASENAME___.swift” and so on… you can add the files you prefer.

The variable “___FILEBASENAME___” will be replaced with the module name that you choose before.

Repeat this operation for all of your options. Your final result must be something like this:

Every file can change dynamically its contents using custom variables. Please refer to the original XCode files if you need inspiration but basically the format is like this:

//
//  ___FILENAME___
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  Copyright (c) ___YEAR___ ___ORGANIZATIONNAME___. All rights reserved.
//

import UIKit

final class ___VARIABLE_moduleName___ViewController: UIViewController {
    
    // @IBOutlet weak var <#name#>: UI<#Type#>!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // ---------------------------------------------------------------------------------------------

    // @IBAction func <#actionName#>(_ sender: <#Any#>) {}

}

Every variable that starts with the 3 underscores, like ___FILENAME___ will be replaced by XCode with the right value.

Customize your file and create your structure.


Adding the template icon

The icons must be named:

  • TemplateIcon@3x.png
  • TemplateIcon@2x.png
  • TemplateIcon.png

No particular limitation about the size, consider that is an icon…

Add these icons to the root folder, the same as the TemplateInfo.plist and the structured folders with your files.


Add custom template to XCode

Well, your template is now ready. You can test it!

Open the template folder, this one:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source

And copy your root folder, the previous one (Custom Swift.xctemplate) to the “Source” folder.

Open Xcode project, create a new file and select your freshly created template. It should works as expected.

You can add the template you want, the UI you need, the continuous integration files, and all of the stuff that you use continuously in your projects.


Enjoy templating!

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.