Create Xcode Templates

Tram Ho

What all Xcode users will be familiar with is the new file creation process. When we create a new file, we will see a window containing a selection of patterns to base the new file on. These new Templates usually contain lines of code that we immediately delete or change to match our style code. Many people may not know that instead of just relying on provided Templates, we can create our own. This can simplify the process of creating new Files and allow them to suit more specific requirements. The process of creating Templates may seem a bit confusing at first, but the end result can be really helpful. We’ll explore the process of writing our own Xcode Templates Files, before diving into how we can share these templates with other developers in a particular project.

Create a Template

When Xcode starts, it will look for Templates files at: ~ / Library / Developer / Xcode / Templates / File Templates. To get started, we need to create a folder here that will contain all of our Templates Files.

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

In this folder, a quick way to get started is to copy one of the built-in Templates and then change it. They can be found in the Xcode application bundle, with a good option for Swift-based Templates being the one for a new Swift file, Source / Swift File.xctemplate.

After copying the files, we see xctemplate contains 4 different files.

  • TemplateIcon.png and [email protected] are thumbnails that are displayed in the new file window. We can use them for all of our Swift Templates or create our own specific Templates.
  • TemplateInfo.plist is where we configure the templates.
  • ___ FILEBASENAME .swift contains a Swift source that creates a base for Templates.

We will start by creating a Template for a new Swift struct, called Swift Struct.xctemplate, based on the Swift File.xctemplate mentioned above.

→ TemplateInfo.plist

By calling the file ___ FILEBASENAME.swift, the resulting Swift file will be named according to what is imported into the new file window. This same argument can be used in Template with FILEBASENAMEASIDENTIFIER. * This means if we import Contact into the new file window, the generated file will be Contact.swift, containing struct Contact.

After the Template is complete, start Xcode and request a new file, we can see our Template being provided in the section named ‘Custom’. The section name is controlled by the directory name we used in ~ / Library / Developer / Xcode / Templates / File Templates.

Other things we can do

While they can be very basic starting out, there are some more important things we can do with Templates.

  • Provide Swift or Objective-C option when creating new file.
  • Change the file name based on what is imported, for example name the file UsersRepositoryTest if importing UsersRepository.
  • It requires entering arguments in the new file window and then using this arguments in code.
  • Add headers or copyright.
  • Add imports or skeleton code to be included in the file by default.

As we explore the capabilities that these templates provide, a problem becomes extremely simple. It would be really great if we could create project-specific templates and share them with other developers in the project. Although the templates for a particular project are not automatically supported by Xcode, it can be accomplished through the ingenious use of symbolic links.

We should put all the templates in a folder in our project, such as FileTemplates. It is possible that the project is kept in source control, which will ensure the templates are tracked and shared with everyone else on the team.

The directory where Xcode is expected to find our templates should already exist if the steps were taken above. If not then we need to make sure it has been created.

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

When the directory exists, a symbolic link can be created here that points to Templates in our Xcode project. It’s worth mentioning that if we delete the project or move it, the symbolic link can simply be deleted and recreated.

For example:

After restarting Xcode, all Project Templates will now be displayed in the ‘New File’ window.

Reference source: Create Xcode file templates and share them with your team

Share the news now

Source : Viblo