In depth mobile app development
Written by Chris Leversuch
Nov 22, 2018

Xcode templates

We've been taking a closer look at Xcode templates - here are some of the things we've discovered.

When creating a new file in an Xcode project, you’re choosing from a list of templates that are bundled with the application.

Templates consist of one or more files with predefined content in them, and a set of configuration options such as the name of the class or, for a UIViewController, whether to also create a XIB file.

Helpfully, you can also create your own templates to cover common file combinations that you regularly build. As with the default templates, you can define your own placeholders, or variables, to substitute values that the user enters during creating.

A good starting point for creating your own templates are the ones that come with Xcode. These can be found in /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File\ Templates/Source and /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File\ Templates/Source

Custom templates should be placed into ~/Library/Developer/Xcode/Templates/. While you can create Project Templates, this blog focuses on File Templates. If the template directory doesn’t exist on your computer, you can create it with:

mkdir -p ~/Library/Developer/Xcode/Templates/File\ Templates

MVP

The first template we’re sharing is for our MVP setup. This contains a number of files that always start with the same basic implementation, so it saves a lot of time to create the feature with a template. The only configuration option we need is the feature name which is used as a prefix on all the file & class names. The template consists of the following files:

  • <feature>Contract.swift - This contains the protocols that our View and Presenter conform to
  • <feature>Presenter.swift - This contains a basic implementation of the Presenter, with an initialiser that takes the view as a parameter and an extension that makes the Presenter conform to the protocol
  • <feature>ViewController.swift - A simple ViewController implementation with an initialiser that creates the presenter and an extension that makes the ViewController conform to the View protocol.
  • <feature>ViewController.xib - An empty xib for building the UI
  • <feature>ViewModel.swift - An empty struct that the Presenter can construct and send to the View

Download from: https://github.com/brightec/Xcode-Templates/tree/master/MVP.xctemplate

Repository

When implementing APIs, we regularly use the repository pattern to separate the remote network calls from the local database queries. This results in a repository class, a local data source class and a remote data source class. This template also just needs a feature name during creation.

  • <feature>.swift - A data model struct
  • <feature>DataSource.swift - The protocol that the repository and data sources conform too
  • <feature>LocalDataSource.swift - A class with an extension to conform to the protocol
  • <feature>RemoteDataSource.swift - A class with an extension to conform to the protocol
  • <feature>Repository.swift - Same as the 2 DataSource files but with an initialiser and properties for keeping references to the data sources

Download from: https://github.com/brightec/Xcode-Templates/tree/master/Repository.xctemplate

Previous Post

Swift Code: UI Testing Tips

Top