Home ios custom templates
Post
Cancel

ios custom templates

Raywenderlich Tutorial

  • ___FILEHEADER___

    This macro is defined by Xcode and populates the file with copyright information, including your name, the current date, and the file name.

1
2
3
4
5
6
//  ___FILENAME___
//  ___PACKAGENAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  ___COPYRIGHT___
//

ViewModel.swift

1
2
3
4
5
6
7
8
//___FILEHEADER___

import Foundation
import Combine

class ViewModel: NSObject, ObservableObject {
  @Published var title = "Hello, world! :]"
}

TemplateInfo.swift

  • 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, c TemplateInfo.plist heckboxes and drop-down lists. Additionally, the Options section determines how to interpret user input.

File Template

Custom Option

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<key>Options</key>
<array>
  <dict>
    <key>Type</key>
    <string>text</string>
    <key>Description</key>
    <string>Raw value for enumeration</string>
    <key>Name</key>
    <string>Raw Value:</string>
    <key>Required</key>
    <true/>
    <key>Identifier</key>
    <string>rawValue</string>
    </dict>
</array>
  • 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.

___FILEBASENAME___.swift

1
2
3
4
5
6
7
8
//___FILEHEADER___

import Foundation

enum ___FILEBASENAMEASIDENTIFIER___: ___VARIABLE_rawValue___ {
    case one
    case two
}

At the top of the file is ___FILEHEADER___, which sets the usual copyright, file and author details.

Next, instead of a name for the enum, you have ___FILEBASENAMEASIDENTIFIER___. This is the same as the ___FILEBASENAME___ variable for the file name, except that you can’t use ___FILEBASENAME___ inside the template file.

Finally, ___VARIABLE_rawValue___ is a reference to the rawValue option you created earlier. The format looks like ___VARIABLE____.

Customizing the file header comment and other text macros in Xcode 9

Xcode 9 File Template

This post is licensed under CC BY 4.0 by the author.