Xcode Project and File Templates

Learn how to create custom project and file templates in Xcode to start new projects and files more efficiently. By Keegan Rush.

Leave a rating/review
Download materials
Save for later
You are currently viewing page 3 of 4 of this article. Click here to view the first page.

Using the Template

Now, you’ll use the template you just set up. First, close Xcode, then reopen it for your changes to take effect.

Go to File ▸ New ▸ Project… to open the New Project dialog. Scroll to the bottom of the iOS category and select your View Model App template. Click Next.

Select the View Model App template while creating a new project in Xcode

On the next screen, set the following values for the options:

  • Product Name: Stellar Space.
  • Organization Identifier: com.raywenderlich.
  • Interface: SwiftUI.
  • Language: Swift.

Click Next, and then Create.

Xcode pop-up to choose options for creating new project

Build and run. You’ll be presented with the classic “Hello, world!” starter screen:

You’ll see the ViewModel.swift file in your project structure. However, ContentView isn’t tied to ViewModel yet. In ContentView.swift, add the following inside ContentView, above body:

@ObservedObject var viewModel = ViewModel()

Next, find Text inside body. Then, replace it with this:


Build and run.

Starter Screen with Label populated using ViewModel data

You’re now pulling the text from the view model.

You’ve created a project with your custom template and used the new ViewModel. Next, you’ll flesh out the rest of Stellar Space.

Populating the Project

You’ll find most of the files for Stellar Space inside the project materials that you downloaded earlier.

Open the project materials in Finder and select the API and UI folders.

Copying API and UI folder from the start project to the current project

Then, drag these into Xcode’s Project navigator.

The copied folders are linked in the current workspace folder in Xcode

Select the Copy items if needed checkbox. Click Finish.

Select Copy items if needed to include new files in the current project

You’ve added a bunch of Swift files to Stellar Space. Later, you’ll use the files in the API folder to communicate to NASA’s API, and you’ll use the files in the UI folder to beautify your ContentView.

Next, select Assets in Xcode’s Project navigator. Delete the empty AppIcon asset.

Delete the default AppIcon givin in the Assets.xcassets folder

Finally, drag AppIcon.appiconset from the project materials into Assets, right next to AccentColor.

Drag AppIcon.appiconset from the project materials into Assets

You’ve just created Stellar Space’s app icon.

Build and run.

A screen showing errors in the project due to API errors

Oh, no! Looks like there’s an APIError object missing from Stellar Space. APIError is an enum that Stellar Space uses to identify a special group of errors. Don’t worry, because next, you’ll learn about Xcode file templates and create the APIError enumeration in the process.

Creating File Templates

So far, you’ve worked with project templates that Xcode uses to create new projects. But Xcode also uses template files when you add a file to an existing project, so now you’ll work on customizing file templates.

Using file templates reduces the amount of boilerplate you need to write. Therefore, you are best to employee custom file templates when you find yourself often creating new files and writing almost identical boilerplate code to start the file off.

Everything you see in the New File dialog is a template, just like in the New Project dialog.

Xcode pop-up to choose a template for creating a new file

APIError is an enum, so you’ll create an Xcode template that makes a single Swift file with an enum inside. That can be useful as you might often create enumerations and having a template to make life easier doing so will reduce your time spent writing boilerplate code.

To create a Swift Enum template for APIError, you’ll copy and modify the default Swift File template.

Copying a Default File Template

Xcode stores its file templates alongside project templates. In Finder, go to Go ▸ Go to Folder… and type in the path to the default file templates:

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

Click Go.

Xcode library content with File Templates

Open the MultiPlatform subfolder and then the Source folder within. Here, you’ll find the Swift File.xctemplate.

Swift file template under Source folder

Swift File.xctemplate is the template that Xcode uses to create a plain Swift file.

It’s a simple template and a great starting point for your enum template as it’s very close to what you need. Copy Swift File.xctemplate to the location where you stored your custom templates:


Copy and Paste the Swift File.xctemplate in the Templates folder

Next, within the custom templates folder, rename Swift File.xctemplate to Swift Enum.xctemplate. When you’re done, the custom templates folder will look like this:

Rename Swift File.xctemplate to Swift Enum.xctemplate

Great! But it’s still the boilerplate Swift File template. So, now, you’ll customize it.

Customizing the File Template Info

You’ll use your new Swift Enum.xctemplate template to create APIError.swift, which contains an enum with an Error raw value. So, it’d be great if your template gave users the option to set a raw value for the enum when creating a file from the template. You’ll do that by adding an option to TemplateInfo.plist.

In Swift Enum.xctemplate, open TemplateInfo.plist with Xcode. Change the values for the following keys:

  • Description: A Swift enum with a raw value.
  • Summary: A Swift enum with a raw value.
  • DefaultCompletionName: Enum.

Description and Summary are used by Xcode in the same way as project templates. However, DefaultCompletionName is new. The value of this key will be the name of any file you create if you don’t rename it. In other words, without changing the file name, the template would name a new file Enum.swift.

At this stage, TemplateInfo.plist looks like this:

TemplateInfo.plist for new Swift file

Now you need to include an option to ask the user to enter a raw value for the enum, such as Error. This will be user input that’s used while generating the new file from the template.

To do so, open TemplateInfo.plist in TextEdit.app or any other text editor. Near the top of the file, after the very first <dict>, add the following:

    <string>Raw value for enumeration</string>
    <string>Raw Value:</string>

Your TemplateInfo.plist will look like this when you’re done:

TemplateInfo.plist after adding custom options to choose while creating a new swift file

You’ve added a new Options array with one dictionary inside it. This will tell Xcode to provide an option when using the template. In this case, the single option added represents a Raw Value option to choose a raw value for the enumeration. Here’s what each item in the dictionary means:

  • Type: The type of user input: text, checkbox or popup.
  • Description: A short description of the option.
  • Name: The name of the option that the user will see in the New File dialog.
  • Required: A boolean value to decide whether or not the user needs to enter a value for the option before continuing.
  • Identifier: A unique identifier to reference the value of the option.

You’re done adding file template info. Now, you need to tell the template how to create the Swift file in the way you need.