Xcode Templates – make it better, do it faster

Apple Xcode Templates

Xcode Templates – make it better, do it faster

During the development process, we are trying to focus on app architecture, patterns to use, best techniques, using 3rd party libs and everything that helps us write less code. We are often improving code, but not processes of writing it.

Сreation of new screens or app modules is our daily routine. No matter if it is architecture pattern VIPER, MVP, MVVM or any other – it always means to create 3 to 6 new files one by one. Smells like monkey job, violation of DRY principle and overtyping, which is equal to wasting time.

Apple created a great tool for automating this process – Xcode templates. Anyhow, for unknown reasons they neither emphasize nor promote this nice feature.

 

Default templates

 

As a first step, take a look at templates Xcode has, that you use on a daily basis. Have you ever noticed word ‘template’ in this form?

 

Xcode Default Templates Window

 

 All of these templates could be found in the next directory.

/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates

Structure of basic Swift template, inside of container with extension .xctemplate, is the following:

  • plist – file, that contains options, additional information.
  • png and TemplateIcon@2x.png that will be shown in template creating form (image above).
  • swift template file itself, that will be customized and created due to selected preferences.

The template can contain as much files as you need. It supports both Objective-C and Swift files, Xibs and Storyboards. Also, if the template provides few options, those files should be grouped in separate folders, named by option name. One template can use as ancestors other templates as well.

The only unpleasant limitation is that Xcode template can’t create folders, you still need to do it manually.

 

Create a custom template

 

It’s better not to touch folder with default templates, and add navigate to the next folder.

~/Library/Developer/Xcode/Templates

If you don’t have Templates folder – create it. All of your templates will be stored here. Also, you can group your templates together by putting them in the same folder. Folder’s name will be the category “header” name.

In this tutorial we are going to create template for MVP+Clean Architecture. This architecture pattern was selected due to its recent use. For easier start, feel free to copy paste Swift File template, rename it. Let’s call it MVP-Configurator.

Creating Xcode Custom Template

Open plist file. Here you can configure your template. First, Identifier key should have unique value, like app bundle id. Second, Kind also String value, can be Xcode.IDEKit.TextSubstitutionFileTemplateKind for file templates or Xcode.Xcode3.ProjectTemplateUnitKind for project template. We always use first one. Next, you can set few more optional fields like Description, DefaultCompletionName, MainTemplateFile etc. So file will look like this:

Plist File Screenshot

Moreover, you can define an array of Options, if you want to provide them during template selection. Each option’s Type can be text, static, checkbox, combo, popup. We’ve found it useful to have options to create module with Xib file, Storyboard or without interface. Type popup is used for it. That configuration will be as on image below.

 

Xcode Template Configuration array of options

 

Please consider, the Identifier key of the option. For Xcode to know that it is the base name you want to use for the files you need to stick to productName. All other variables – feel free to name as you wish.

Once it’s done, we can proceed with fulfilling the source code files. Lets understand how it works on example of ViewController.

 

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

import UIKit

final class ___VARIABLE_productName___ViewController: UIViewController {

    // MARK: - Properties

    var presenter: ___VARIABLE_productName___Presenter!
    var configurator: ___VARIABLE_productName___Configurator!
    
    // MARK: - Lifecycle

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

// MARK: - ___VARIABLE_productName___View

extension ___VARIABLE_productName___ViewController: ___VARIABLE_productName___View {
}

 

All macroses used in file header are provided as it is in base Swift template. Unfortunately, Apple documentation about templates is almost missing. Read more about available macroses here.

___VARIABLE_productName___ is a custom variable that you input in Name field during template creation. As we need only one variable, it will be used in all our files. You can define as many options as you need  in TemplateInfo.plist. Each variable name is placed by key Identifier, but to access it in source files it should be in the format as shown ___VARIABLE_yourVariableName___.

Also, there is very detailed but very outdated short book, made by an enthusiast about creating templates on Xcode 4. Anyhow, most articles, and this one is not an exception, are based on that information.

You can freely access the whole template on Sceel.io GitHub repository.

 

Conclusion

 

After creating templates for files that you often used to create manually, now you will be rewarded with more free time. Thus you can use it for making awesome features in your app.

Remember, the template should be universal for one exact “building block” of your app, is it one class or whole screen module. It can’t solve everything, so don’t overcomplicate it. Once you get more practice with them, you’ll find templates as hidden treasure. It truly helps not to focus on boilerplate code.

We think, if Harry Potter was a developer, he’d definitely use the spell “Templato Creo”!