Xcode Project and File Templates
- Getting Started
- Understanding Default Templates
- Exploring Base Templates
- Exploring iOS Templates
- Creating Your First Project Template
- Contents of a Template
- Customizing the Project Template
- Working With Project Template Info
- Exploring Options
- Adding the View Model
- Using the Template
- Populating the Project
- Creating File Templates
- Copying a Default File Template
- Customizing the File Template Info
- Creating Template Files
- Using the Template
- Finishing the Project
- Where to Go From Here?
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.
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.
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
@ObservedObject var viewModel = ViewModel()
body. Then, replace it with this:
Build and run.
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.
Then, drag these into Xcode’s Project navigator.
Select the Copy items if needed checkbox. Click Finish.
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
Next, select Assets in Xcode’s Project navigator. Delete the empty AppIcon asset.
Finally, drag AppIcon.appiconset from the project materials into Assets, right next to AccentColor.
You’ve just created Stellar Space’s app icon.
Build and run.
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.
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:
Open the MultiPlatform subfolder and then the Source folder within. Here, you’ll find the Swift File.xctemplate.
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:
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:
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.
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:
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>
Your TemplateInfo.plist will look like this when you’re done:
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:
- 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.