Want to offer native, in-app customer service to your users? Get going quickly with Zendesk’s Mobile SDKs. Free with Zendesk.


#89: Custom Xcode File Templates πŸ”¨


Xcode ships with some great starter templates for projects, files, and even targets. Today we'll look at how to add our own custom file templates to Xcode's built-in set. We'll be adding a new template for easily creating new table view controllers powered by Static (a static table view library we covered in Bite #74).

We'll begin by creating a folder for our custom templates to live in. This will be: ~/Library/Developer/Xcode/Templates/File Templates/Custom. We'll create a new folder in here called Static Table View Controller.xctemplate.

Then we'll need to create two files: One called TemplateInfo.plist to describe our new template, and another where our actual template content will live.

Let's start with TemplateInfo.plist. We'll create this using Xcode by selecting File > New > File... and then Resources > Property List. We'll call it TemplateInfo.plist (the name is important) and give it this content:

Then we'll select File > New > File... and select Source > Swift File. We'll name it __FILEBASENAME__.swift to match the value in our TemplateInfo.plist.

The template's content almost looks like normal Swift code, but contains template tags (which will get replaced and "filled in" at run time):

//  Created by ___FULLUSERNAME___ on ___DATE___. ___COPYRIGHT___

import UIKit
import Static

class ___FILEBASENAMEASIDENTIFIER___: TableViewController {
  override func viewDidLoad() {

    dataSource.sections = [ Section(rows: [ Row(text: "Hello World") ]) ]

Lastly, we'll add an icon for our new template by adding a TemplateIcon.png our template folder.

We can now press ⌘N in Xcode to see our new template. Success!

The example template we made here is available for download here.

Update: We cover custom project templates in Bite #90.