XCode Templates tutorial – How To Create Custom Template Step By Step

With Xcode, every day we create files and groups. As a developer improving work processes is always on their mind. We need tools and solutions to speed up the coding, testing, or organizing of our work. We usually create files for our classes, storyboards, or XIBs. We organize them into folders to have a logically organized project. Our preferred IDE provides several useful built-in templates that we can use to create various types of projects or files. I believe this article will be useful in improving your daily tasks as well.

What are the XCode templates?

XCode Templates is a tool for making code snippets to give you a better starting point to accomplish your task with fulfill your needs and speed up the work. In this article, we will be preparing a custom template for MVVM project architecture.

Often, we need to make from scratch the structure and files for a new module, and this process more similar every time. For instance, in the MVVM pattern, to build a Login module we need to create folders and at least four classes:

  1. Login Module Folder.
  2. Login View.
  3. Login View Model.
  4. Login View Controller.
  5. Login Model.

Adding each class in your necessary code is time-consuming. To avoid this we adding XCode templates to our project. Let’s see how to configure a template for use with a new MVVM module.

Installation of XCode Templates

Initially, we need to add a new folder which will consist of our custom templates for install in XCode templates.

XCode iOS templates location

Each Xcode custom template files are located in  ~/Library/Developer/Xcode/Templates/  and grouped into segments by folder name. You can add it manually or using the terminal by running the following command:

mkdir ~/Library/Developer/Xcode/Templates/Custom Templates

File template structure

The main folder for XCode templates

Every XCode file template is a distinct folder with the extension .xctemplate. If you need a template named “View, Model & ViewModel”, you should create a folder named “View, Model & ViewModel.xctemplate in “~/Library/Developer/Xcode/Templates/File Templates/Custom Templates”.

Internal folders and files in the XCode templates















Module Name


                              The name of the Model, View and ViewModel to create







The TemplateInfo.plist file consists of the description of the basic template, implementation gives us the ability to type Module name while creating XCode app templates with the wizard. You can’t need to put pretty much anything into the actual template file. Text macros like ___FILEBASENAME___ to refer to the filename. The name is fetched from the productName option from our TemplateInfo.info file which is set in the new file wizard.

Implementation and structure

XCode Templates tutorial – How To Create Custom Template Step By Step

1. Implementation of viewcontroller

import UIKit

class ___FILEBASENAMEASIDENTIFIER___: UIViewController {

 let viewModel: ___VARIABLE_productName___ViewModel

    let mainView: ___VARIABLE_productName___View

    init() {

        viewModel = ___VARIABLE_productName___ViewModel(withModel: ___VARIABLE_productName___())

        mainView = ___VARIABLE_productName___View()

        super.init(nibName: nil, bundle: nil)

        mainView.configure(withViewModel: viewModel) }

    required init?(coder _: NSCoder) {

        fatalError(“init(coder:) has not been implemented”) }

  override func viewDidLoad() {


        setupView() }

       private func setupView() {


        mainView.snp.makeConstraints {

            make in

            make.top.leading.trailing.bottom.equalToSuperview() } }}

 As you can see we implemented the following:

  • Declaring variables of viewModel and mainView. It will consist of our productName from TemplateInfo.plist set in file wizard.
  • Variables initializing.
  • Default initializer.
  • Required initializer.
  • viewDidLoad implementation.
  • setupView function adding mainView and set SnapKit constraints.

2. Implementation of model

import Foundation


This class will be generated automatically by XCode so the above is just an instance.

3. Implementation of view

import UIKit


     init() { super.init(frame: CGRect.zero) }

   required init?(coder _: NSCoder) {

        fatalError(“init(coder:) has not been implemented”) }

    func configure(with viewModel: ___VARIABLE_productName:identifier___ViewModel) {

        // configure the view with a ___VARIABLE_productName:identifier___ViewModel }}

Class view consist of default initializer, and a required initializer, both required to initialize the View from the code. We also need to configure function to bind viewModel and the view. Note that ViewModel name is the same as the ViewModel name in the ViewController.

4. Implementing viewmodel

import Foundation


    private let model: ___VARIABLE_productName:identifier___

    init(withModel model: ___VARIABLE_productName:identifier___) {

        self.model = model    }}

ViewModel is initialized with our Model created in p.2.

How to use files in XCode templates?

Step 1: To get started you require to click File -> New -> File and Find your template in the list.

XCode Templates tutorial – How To Create Custom Template Step By Step

Step 2: Next enter the model name.

XCode Templates tutorial – How To Create Custom Template Step By Step

Step 3: Then add it to your project.

XCode Templates tutorial – How To Create Custom Template Step By Step
XCode Templates tutorial – How To Create Custom Template Step By Step

More options in Xcode templates

Some example files available on GitHub, you can check them. If you need to improve custom templates for better usage you can also reverse engineer Xcode default templates,  it’s located in Xcode.app/Contents/Developer/Library/Xcode/Templates or Xcode.app/Contents/Developer/Platforms//Developer/Library/Xcode/Templates.


In this article, we cover the XCode Templates tutorial this will help you to speed up your coding. You can modify any part code for your needs.