Create Your Own Xcode File Templates and Reduce Development Time

Batikan Sosun
4 min readMay 10, 2020

--

I recently asked myself, “How can I shorten the development time” since some projects have been delayed.

Imagine that you are developing a project and assume it consists of 50 screen classes. You repeatedly write down the codes, methods, and functions that should be for each screen class, which is quite annoying and time-consuming.

I want that when I create a screen class with any design architecture, all the sub-structures related to this screen class are created automatically.

For example; When I create the Movie List screen using the MVVM architecture, the service methods, view models, service models, views, protocols and data source or data adapters are automatically created.

Sounds good, right?

The Good news is Xcode provides that all. Let’s take a look — Xcode File Template — ViewController which is a cocoa touch class.

Like that, when you create a cocoa touch controller named ViewController this controller contains imported UIKit Framework, created class named ViewController and that inherited from UIViewController and this class required a method which name is viewDidLoad and then that calls their superclass.

In short, a view controller template can be created like the above summary.

Now, we will create an MVVM file Module as the Xcode File Template together.

Make sure this will contribute to your work a lot. It will improve your development processes and reduce your development time.

Before that, you can discover existing The Xcode Template(The built-in file and project templates).

All Xcode Templates are located here:

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

Run below code on your Terminal

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

and then run

open .

The Finder will be open and then you can see File Templates and Project Templates.

The MVVM structure we will create will look like the below.

  • __FILABASENAME__
  • Model
  • ___FILEBASENAME___Model.swift
  • Protocol
  • ___FILEBASENAME___ServiceProtocol.swift
  • Service
  • ___FILEBASENAME___Service.swift
  • View
  • ___FILEBASENAME___View.swift
  • View Model
  • ___FILEBASENAME___ViewModel.swift

In the Xcode File Organizer, the structure will be like below:

And expanded will be like below:

Now, let’s get back to our work.

All the Xcode custom template files are located in this path ~/Library/Developer/Xcode/Templates/ and grouped into sections by folder name. You can add it manually or using the terminal.

Each Xcode file template is a separate folder with this extension .xctemplate. If you want to create a template named “New MVVM Module”, you have to create a folder named “New MVVM Module.xctemplate” in ~/Library/Developer/Xcode/Templates/

we will add our custom file templates in the folder: ~/Library/Developer/Xcode/Templates/

And the final structure will be like the below screenshot.

In this structure;

__FILEBASENAME__ Given the module name and folder contains all source files.

TemplateIcon.png and TemplateIcon@2x.png is the template’s icon.

Also, TemplateInfo.plist manages the template files. It will be like below:

And another screenshot, more readable

— The Kind value determines whether this will be a file template.

— Platform as you know iPhoneOS.

And options array detailed about the module.

If you have done all the steps in order, your new module is ready.

I will describe the methods, functions and protocols contained in the files in the next article.

Xcode File Template’s all file here: https://github.com/batikansosun/Xcode-File-Template

Conclusion

Sometimes we spend a lot of time on development processes. We do the same things, we lose a lot of time and we need to manage our time well and use the tools that Xcode provides us. Based on this sample, I told you about the Xcode File Template.

If you have any questions, please feel free.

Write a comment below.

--

--

Batikan Sosun
Batikan Sosun

Written by Batikan Sosun

Tweeting tips and tricks about #swift #xcode #apple Twitter @batikansosun Weekly Swift Blogging

Responses (2)