In this tutorial, you’ll learn:
- What Xcode templates are and where to find the default templates that ship with Xcode.
- The anatomy of Xcode’s templates.
- How to create project templates.
- How to create file templates.
- Getting input from users when they use a template.
Understanding Default Templates
Xcode’s rich and complex templating system also allows user input, conditional logic and many combinations of files. Each template also offers inheritance, meaning the templates you create can adopt logic from one another or the default templates.
Exploring Base Templates
/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/Project Templates
Exploring iOS Templates
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS
Creating Your First Project Template
There are three basic steps:
- Copy an existing template. In this case,
App.xctemplate
. - Create a folder to store all your custom templates.
- Modify the copied template to match your needs.
Open a new Finder window. Press Command-Shift-G or go to Go ▸ Go to Folder… and type in the path to the Xcode folder in your user library:
~/Library/Developer/Xcode
Most importantly, for now, it also stores your custom templates. It is in your home directory and therefore is specific to your user account on the machine.
To create new custom template subfolder, right-click in this folder and then click New Folder. Name the subfolder Templates. Then, open the new Templates subfolder.
This folder will store your custom templates. Now, right-click in the folder and click Paste Item to paste the copied App.xctemplate.
Next, rename the item you just pasted from App.xctemplate to View Model App.xctemplate.
To make your template work, you’ll need to give it an unique identifier. Inside View Model App.xctemplate
, open TemplateInfo.plist
with Xcode.
Contents of a Template
Working With Project Template Info
TemplateInfo.plistBelow are some essential keys you’ll find inside the property list:
-
Kind
: The type of template. In this case it is Xcode.Xcode3.ProjectTemplateUnitKind, which means it’s a project template. -
Identifier
: A unique identifier for your template. -
Ancestors
: A list of identifiers of other templates that this template inherits from. You’ll see two items in this array: com.apple.dt.unit.coreDataCocoaTouchApplication and com.apple.dt.unit.sceneLifecycleApplication. -
Concrete
: Indicates if the template is available directly while creating a new project in Xcode. Since you want to use this template to create a custom project, set it to true. If set to false, you can’t use the template directly as it will be an abstract base template from which your other templates can inherit information. -
Description
: A short description of the template. -
SortOrder
: Usually, Xcode arranges templates alphabetically, but you can set the sort order here to override the alphabetical sorting. -
NameOfInitialFileForEditor
: Indicates the file that Xcode will display once the new project is ready. -
Options
: A list of different dialog options in the New Project dialog. Options is the most complex as well as the most important of all the keys. Using Options, you can ask the user for input in the form of text fields, checkboxes and drop-down lists. Additionally, the Options section determines how to interpret user input.
Exploring Options
Here’s what’s inside the Item 1 dictionary:
Item 1-
Identifier
: A unique identifier to reference the option. -
Name
: The name of the option that the user will see in the New Project dialog. -
Description
: A short description of the option’s purpose. -
Values
: A list of possible values for popup type options. -
Default
: A default value in case the user doesn’t choose one.
Type: The type of the option, eitherpopup
,checkbox
ortext
.
Adding the View Model
Add UnitsHere’s what’s in the items above:
- Units is a list of values for the option, along with the behavior for each value.
- Each item in the dictionary is a Value that the user will see in the New Project dialog. Currently, the values are SwiftUI and Storyboard, which are the same as the old Values.
- Under the SwiftUI value, there’s a dictionary for your added behavior.
- Nodes is a list of files that will be included if the user chooses the SwiftUI value as the option for userInterface.
- Definitions contains the actual path for any files in Nodes.
A Storyboard value that’s the same as the one in the old Values, without any behavior.
Populating the Project
Creating File Templates
Copying a Default File Template
/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates
Creating Template Files
Inside ___FILEBASENAME___.swift
, replace the contents with this:
//___FILEHEADER___
import Foundation
enum ___FILEBASENAMEASIDENTIFIER___: ___VARIABLE_rawValue___ {
case one
case two
}
[Reference]
https://www.raywenderlich.com/26582967-xcode-project-and-file-templates
网友评论