🌟

Xcodeテンプレートのドキュメント

に公開

Xcodeテンプレートの作成方法

概要

Xcodeテンプレート(.xctemplateファイル)は、新しいファイルやプロジェクトを作成する際に使用される設定ファイルです。/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/に配置されており、カスタムテンプレートを作成することで開発効率を向上させることができます。

ディレクトリ構造

Xcodeテンプレートは以下の構造で配置されています:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/
├── File Templates/           # ファイルテンプレート
│   ├── iOS/
│   └── User Interface/
└── Project Templates/        # プロジェクトテンプレート
    └── iOS/

テンプレートの種類

1. ファイルテンプレート(File Templates)

単一のファイル作成に使用されるテンプレートです。

2. プロジェクトテンプレート(Project Templates)

新しいプロジェクト作成時に使用されるテンプレートです。

テンプレートファイルの基本構造

Xcodeテンプレートは.xctemplate拡張子を持つディレクトリとして作成されます。基本的な構造は以下の通りです:

MyTemplate.xctemplate/
├── TemplateInfo.plist        # テンプレート設定ファイル
├── TemplateIcon-1016.png     # テンプレートアイコン(1016x1016)
├── TemplateIcon-1016@2x.png  # テンプレートアイコン(2032x2032)
├── ___FILEBASENAME___.swift  # テンプレートファイル
└── SubclassSpecific/         # サブクラス固有のファイル
    ├── UIViewSwift/
    └── UIViewControllerSwift/

TemplateInfo.plistの詳細設定項目

基本構造と必須項目

Kind

テンプレートの種類を定義する最重要項目です。

<key>Kind</key>
<string>Xcode.IDEFoundation.TextSubstitutionFileTemplateKind</string>

利用可能な値:

  • Xcode.IDEFoundation.TextSubstitutionFileTemplateKind: ファイルテンプレート
  • Xcode.Xcode3.ProjectTemplateUnitKind: プロジェクトテンプレート

Identifier(プロジェクトテンプレート必須)

テンプレートの一意識別子を設定します。

<key>Identifier</key>
<string>com.apple.dt.unit.application.SwiftUI.iOS</string>

Description

テンプレートの詳細説明文です。

<key>Description</key>
<string>This template provides a starting point for an application that uses a single view.</string>

Summary(ファイルテンプレート推奨)

テンプレートの短い概要です。

<key>Summary</key>
<string>A Cocoa Touch class</string>

Platforms

対象プラットフォームを配列で指定します。

<key>Platforms</key>
<array>
    <string>com.apple.platform.iphoneos</string>
</array>

利用可能なプラットフォーム識別子:

  1. com.apple.platform.iphoneos - iOS プラットフォーム

    • iOSアプリ、フレームワーク、拡張機能に使用
  2. com.apple.platform.macosx - macOS プラットフォーム

    • macOSアプリ、フレームワーク、コマンドラインツールに使用
  3. com.apple.platform.watchos - watchOS プラットフォーム

    • Apple Watchアプリと拡張機能に使用
  4. com.apple.platform.appletvos - tvOS プラットフォーム

    • Apple TVアプリと拡張機能に使用
  5. com.apple.platform.xros - visionOS プラットフォーム

    • Vision Proアプリと拡張機能に使用
  6. com.apple.platform.driverkit - DriverKit プラットフォーム

    • DriverKitドライバーとフレームワークに使用

マルチプラットフォーム対応例:

<key>Platforms</key>
<array>
    <string>com.apple.platform.iphoneos</string>
    <string>com.apple.platform.macosx</string>
</array>

重要な注意点:

  • すべてcom.apple.platform.*の形式で指定
  • プラットフォーム名は基本的に小文字(macOSはmacosx、visionOSはxros
  • 複数プラットフォームに対応する場合は配列に複数の識別子を含める
  • Extension用テンプレートではAllowablePlatformsキーでも使用される

アーキテクチャ設定

Ancestors

テンプレートの継承システムです。他のテンプレートから機能を継承できます。

<key>Ancestors</key>
<array>
    <string>com.apple.dt.unit.cocoaTouchApplicationBase</string>
    <string>com.apple.dt.unit.storageType</string>
</array>

Concrete

このテンプレートが実際に使用可能かを指定します。

<key>Concrete</key>
<true/>

プロジェクト設定

Project

プロジェクト全体の設定を定義します。

<key>Project</key>
<dict>
    <key>SDK</key>
    <string>iphoneos</string>
    <key>SharedSettings</key>
    <dict>
        <key>IPHONEOS_DEPLOYMENT_TARGET</key>
        <string>latest_iphoneos</string>
        <key>SWIFT_VERSION</key>
        <string>5.0</string>
    </dict>
</dict>

Targets

ビルドターゲットの詳細設定を配列で定義します。

<key>Targets</key>
<array>
    <dict>
        <key>TargetIdentifier</key>
        <string>com.apple.dt.widgetExtension.iOS</string>
        <key>Name</key>
        <string>___PACKAGENAME___Extension</string>
        <key>Frameworks</key>
        <array>
            <string>WidgetKit</string>
            <string>SwiftUI</string>
        </array>
        <key>SharedSettings</key>
        <dict>
            <key>ENABLE_PREVIEWS</key>
            <string>YES</string>
            <key>INFOPLIST_KEY_UIApplicationSceneManifest_Generation</key>
            <string>YES</string>
        </dict>
    </dict>
</array>

AssociatedTargetSpecification

関連ターゲットの設定(Extensionテンプレート等で使用)

<key>AssociatedTargetSpecification</key>
<dict>
    <key>PopUpTitle</key>
    <string>Embed in Application:</string>
    <key>PopUpDescription</key>
    <string>The application target that will host this new extension.</string>
    <key>AllowableProductTypes</key>
    <array>
        <string>com.apple.product-type.application</string>
    </array>
    <key>AllowablePlatforms</key>
    <array>
        <string>com.apple.platform.iphoneos</string>
    </array>
    <key>AssociatedTargetIsDependent</key>
    <true/>
    <key>AssociatedTargetNeedsProductBuildPhaseInjection</key>
    <true/>
</dict>

表示設定

SortOrder

テンプレート一覧での表示順序を数値で指定します。

<key>SortOrder</key>
<integer>2</integer>

DefaultCompletionName

デフォルトのファイル名を指定します。

<key>DefaultCompletionName</key>
<string>MyClass</string>

NameOfInitialFileForEditor

作成後に最初に開くファイルを指定します。

<key>NameOfInitialFileForEditor</key>
<string>ContentView.swift</string>

SupportsSwiftPackage

Swift Packageでの使用をサポートするかを指定します。

<key>SupportsSwiftPackage</key>
<true/>

Options配列の高度な設定

ユーザーが選択できるオプションをOptions配列で定義します。

基本オプション構造

テキスト入力オプション

<dict>
    <key>Identifier</key>
    <string>productName</string>
    <key>Required</key>
    <true/>
    <key>Name</key>
    <string>Class:</string>
    <key>Description</key>
    <string>The name of the class to create</string>
    <key>Type</key>
    <string>text</string>
    <key>NotPersisted</key>
    <true/>
</dict>

プルダウンメニューオプション

<dict>
    <key>Identifier</key>
    <string>languageChoice</string>
    <key>Required</key>
    <true/>
    <key>Name</key>
    <string>Language:</string>
    <key>Description</key>
    <string>The implementation language</string>
    <key>Type</key>
    <string>popup</string>
    <key>Default</key>
    <string>Swift</string>
    <key>Values</key>
    <array>
        <string>Swift</string>
        <string>Objective-C</string>
    </array>
    <key>MainTemplateFiles</key>
    <dict>
        <key>Swift</key>
        <string>___FILEBASENAME___.swift</string>
        <key>Objective-C</key>
        <string>___FILEBASENAME___.m</string>
    </dict>
    <key>AllowedTypes</key>
    <dict>
        <key>Swift</key>
        <array>
            <string>public.swift-source</string>
        </array>
        <key>Objective-C</key>
        <array>
            <string>public.objective-c-source</string>
            <string>public.objective-c-plus-plus-source</string>
        </array>
    </dict>
</dict>

チェックボックスオプション

<dict>
    <key>Identifier</key>
    <string>XIB</string>
    <key>Name</key>
    <string>Also create XIB file</string>
    <key>Description</key>
    <string>Whether to create a XIB file with the same name</string>
    <key>Type</key>
    <string>checkbox</string>
    <key>Default</key>
    <string>false</string>
    <key>RequiredOptions</key>
    <dict>
        <key>cocoaTouchSubclass</key>
        <array>
            <string>UIViewController</string>
            <string>UITableViewController</string>
        </array>
    </dict>
</dict>

クラス選択オプション

<dict>
    <key>Identifier</key>
    <string>cocoaTouchSubclass</string>
    <key>Required</key>
    <string>YES</string>
    <key>Name</key>
    <string>Subclass of:</string>
    <key>Description</key>
    <string>What class to subclass in the new file</string>
    <key>Type</key>
    <string>class</string>
    <key>Default</key>
    <string>NSObject</string>
    <key>Values</key>
    <array>
        <string>NSObject</string>
        <string>UIViewController</string>
        <string>UIView</string>
    </array>
    <key>Suffixes</key>
    <dict>
        <key>UIViewController</key>
        <string>ViewController</string>
        <key>UIView</key>
        <string>View</string>
    </dict>
</dict>

高度なオプション制御

RequiredOptions - 条件付き表示

他のオプションの値によってこのオプションの表示を制御します。

<key>RequiredOptions</key>
<dict>
    <key>storageType</key>
    <string>Core Data</string>
</dict>

RequiredOptionsForValues - 特定値の条件制御

特定の値が選択された場合の条件を設定します。

<key>RequiredOptionsForValues</key>
<dict>
    <key>Swift Testing</key>
    <dict>
        <key>languageChoice</key>
        <string>Swift</string>
    </dict>
</dict>

Override - 値の強制設定

特定の条件下でオプションの値を強制的に設定します。

<key>Override</key>
<string>Swift</string>

NotPersisted - 設定の非永続化

このオプションの値をXcodeの設定に保存しません。

<key>NotPersisted</key>
<true/>

Units システム - 条件付きコンテンツ生成

オプション値に基づいて異なるファイルセットやビルド設定を適用する強力な機能です。

<key>Units</key>
<dict>
    <key>Swift</key>
    <array>
        <dict>
            <key>Nodes</key>
            <array>
                <string>___PACKAGENAME:identifier___App.swift</string>
                <string>ContentView.swift</string>
                <string>Assets.xcassets</string>
            </array>
            <key>Definitions</key>
            <dict>
                <key>ContentView.swift</key>
                <dict>
                    <key>Path</key>
                    <string>ContentView.swift</string>
                    <key>SortOrder</key>
                    <integer>99</integer>
                </dict>
                <key>Assets.xcassets</key>
                <dict>
                    <key>Path</key>
                    <string>Assets.xcassets</string>
                    <key>AssetGeneration</key>
                    <array>
                        <dict>
                            <key>Type</key>
                            <string>appicon</string>
                            <key>Name</key>
                            <string>AppIcon</string>
                            <key>Platforms</key>
                            <dict>
                                <key>iOS</key>
                                <string>true</string>
                            </dict>
                        </dict>
                    </array>
                </dict>
            </dict>
            <key>Targets</key>
            <array>
                <dict>
                    <key>SharedSettings</key>
                    <dict>
                        <key>ENABLE_PREVIEWS</key>
                        <string>YES</string>
                    </dict>
                </dict>
            </array>
        </dict>
    </array>
</dict>

テンプレート変数とファイル管理

システム定義変数

テンプレートファイル内で使用できる組み込み変数です。

基本変数

  • ___FILEBASENAME___: 拡張子を除いたファイル名
  • ___FILEBASENAMEASIDENTIFIER___: 識別子として有効なファイル名
  • ___FILEHEADER___: ファイルヘッダーコメント
  • ___VARIABLE_optionIdentifier___: オプションで定義した変数の値

プロジェクト変数

  • ___PACKAGENAME___: パッケージ名
  • ___PACKAGENAME:identifier___: 識別子形式のパッケージ名
  • ___PACKAGENAMEASIDENTIFIER___: アンダースコア区切り識別子
  • ___TARGETNAME___: ターゲット名
  • ___TARGETNAME:c99ExtIdentifier___: C99互換識別子

Nodes - 生成ファイル定義

生成するファイルやコンテンツを指定します。

<key>Nodes</key>
<array>
    <string>Info.plist:NSExtension</string>
    <string>Assets.xcassets</string>
    <string>___PACKAGENAME:identifier___.swift</string>
</array>

Definitions - ファイル詳細設定

Nodesで指定したファイルの詳細設定を定義します。

<key>Definitions</key>
<dict>
    <key>Assets.xcassets</key>
    <dict>
        <key>Path</key>
        <string>Assets.xcassets</string>
        <key>AssetGeneration</key>
        <array>
            <dict>
                <key>Type</key>
                <string>appicon</string>
                <key>Name</key>
                <string>AppIcon</string>
                <key>Platforms</key>
                <dict>
                    <key>iOS</key>
                    <string>true</string>
                </dict>
            </dict>
        </array>
        <key>SortOrder</key>
        <integer>100</integer>
    </dict>
    <key>___PACKAGENAME:identifier___.swift</key>
    <dict>
        <key>Path</key>
        <string>___PACKAGENAME:identifier___.swift</string>
        <key>BuildAttributes</key>
        <array>
            <string>Public</string>
        </array>
    </dict>
</dict>

Components - サブテンプレート統合

他のテンプレートを組み込む機能です。

<key>Components</key>
<array>
    <dict>
        <key>Identifier</key>
        <string>com.apple.dt.unit.iosFrameworkUnitTestBundle</string>
        <key>Name</key>
        <string>___PACKAGENAME___Tests</string>
    </dict>
</array>

コードテンプレート定義

特定のコードブロックを定義して再利用できます。

<key>*:implementation:viewDidLoad</key>
<dict>
    <key>Beginning</key>
    <string>override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.</string>
    <key>End</key>
    <string>}
</string>
    <key>Indent</key>
    <integer>1</integer>
</dict>

使用例

//___FILEHEADER___

import UIKit

class ___FILEBASENAMEASIDENTIFIER___: ___VARIABLE_cocoaTouchSubclass___ {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

条件分岐ディレクトリ

選択されたオプションに応じて異なるファイルを生成する場合、条件分岐ディレクトリを使用します。

命名規則

{ClassName}{Language}{AdditionalOption}の形式でディレクトリ名を作成します。

例:

  • UIViewControllerSwift/
  • UIViewControllerObjective-C/
  • UIViewControllerXIBSwift/

構造例

MyTemplate.xctemplate/
├── TemplateInfo.plist
├── UIViewControllerSwift/
│   └── ___FILEBASENAME___.swift
├── UIViewControllerObjective-C/
│   ├── ___FILEBASENAME___.h
│   └── ___FILEBASENAME___.m
└── UIViewControllerXIBSwift/
    ├── ___FILEBASENAME___.swift
    └── ___FILEBASENAME___.xib

アイコンファイルの詳細設定

ファイルテンプレート用アイコン

ファイルテンプレートでは、テンプレートディレクトリに直接アイコンファイルを配置します:

MyFileTemplate.xctemplate/
├── TemplateIcon-1016.png      # 標準解像度アイコン(48x48ピクセル)
├── TemplateIcon-1016@2x.png   # 高解像度アイコン(96x96ピクセル)
└── TemplateInfo.plist

重要な注意点:

  • ファイル名は「1016」となっていますが、実際のサイズは48x48ピクセル(@2xは96x96ピクセル)です
  • PNGフォーマットで作成する必要があります
  • 透明背景(RGBA)に対応しています
  • アイコンが存在しない場合、Xcodeはデフォルトのアイコンを表示します

プロジェクトテンプレート用アイコン

プロジェクトテンプレートでは、TemplateInfo.plist内でアイコンを指定する方法も利用できます:

1. 直接ファイル指定方式(ファイルテンプレートと同様)

MyProjectTemplate.xctemplate/
├── TemplateIcon-1016.png      
├── TemplateIcon-1016@2x.png   
└── TemplateInfo.plist

2. Bundle内アイコン参照方式

TemplateInfo.plist内で、Xcodeの内部アイコンバンドルを参照する方式:

<key>Image</key>
<dict>
    <key>BundleImageName</key>
    <string>app</string>
    <key>BundleIdentifier</key>
    <string>com.apple.dt.IDEKit</string>
</dict>

利用可能なBundleImageNameの例:

  • app: アプリケーションアイコン
  • framework: フレームワークアイコン
  • unittest: ユニットテストアイコン
  • widget: ウィジェットアイコン
  • flyingsaucer: ゲームアイコン
  • appclip: App Clipアイコン
  • matter: Matterアイコン
  • classkit: ClassKitアイコン
  • app.intent: App Intentsアイコン
  • bolt.shield: Shield Actionアイコン

アイコン作成のガイドライン

デザイン要件

  1. サイズ: ファイルテンプレート用は48x48px、@2x版は96x96px
  2. フォーマット: PNG形式(RGBA対応)
  3. 背景: 透明背景を推奨
  4. カラー: Xcodeのダークモード・ライトモード両対応を考慮

デザインのベストプラクティス

  1. シンプルで認識しやすいデザイン: 小さなサイズでも判別できるよう、シンプルなアイコンにする
  2. コントラストを確保: ダークモード・ライトモード両方で視認性を保つ
  3. 既存アイコンとの差別化: Xcode標準のテンプレートアイコンと混同されないデザイン
  4. 一貫性: 関連するテンプレート群で統一されたデザイン言語を使用

アイコンの優先順位

Xcodeは以下の優先順位でアイコンを探します:

  1. TemplateIcon-1016@2x.png (Retinaディスプレイ用)
  2. TemplateIcon-1016.png (標準ディスプレイ用)
  3. Bundle内アイコン (TemplateInfo.plistで指定されている場合)
  4. デフォルトアイコン (何も指定されていない場合)

カスタムアイコンの作成例

1. デザインツールでの作成

  • Sketch、Figma、Photoshop等で48x48pxのキャンバスを作成
  • 透明背景でアイコンをデザイン
  • 96x96pxでも同じデザインを作成(@2x用)

2. コマンドラインでの変換

ImageMagickを使用してアイコンをリサイズ:

# 元画像を48x48にリサイズ
magick input.png -resize 48x48 TemplateIcon-1016.png

# 元画像を96x96にリサイズ  
magick input.png -resize 96x96 TemplateIcon-1016@2x.png

3. SF Symbolsの活用

AppleのSF Symbolsから適切なシンボルを選んで、一貫したデザインを作成:

# SF Symbols.appで適切なシンボルを探し、エクスポートして使用

トラブルシューティング

アイコンが表示されない場合

  1. ファイル名の確認: TemplateIcon-1016.pngの命名が正確か
  2. ファイルサイズの確認: 実際に48x48px(@2xは96x96px)になっているか
  3. ファイル形式の確認: PNG形式で保存されているか
  4. Xcodeの再起動: テンプレートキャッシュをクリアするため

アイコンがぼやける場合

  1. @2x版の確認: 高解像度版(96x96px)が正しく作成されているか
  2. ベクター化: 可能な限りベクター形式で作成してからラスタライズ
  3. ピクセルパーフェクト: エッジがピクセル境界に合っているか確認

カスタムテンプレートの作成手順

1. テンプレートディレクトリの作成

mkdir ~/Desktop/MyCustomTemplate.xctemplate

2. TemplateInfo.plistの作成

基本的な設定を含むplistファイルを作成します。

3. テンプレートファイルの作成

実際に生成されるファイルのひな形を作成します。

4. アイコンファイルの追加(オプション)

独自のアイコンを使用する場合は、適切なサイズのPNGファイルを追加します。

5. テンプレートのインストール

作成したテンプレートを適切なディレクトリにコピーします:

# ファイルテンプレートの場合
cp -r MyCustomTemplate.xctemplate "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/iOS/Source/"

# プロジェクトテンプレートの場合
cp -r MyCustomTemplate.xctemplate "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application/"

注意事項

  1. Xcodeを再起動するとテンプレートが認識されます
  2. システムのXcodeアップデート時にカスタムテンプレートが削除される可能性があります
  3. バックアップを取っておくことを推奨します
  4. テンプレート変数は大文字小文字を区別します
  5. plistファイルの構文エラーがあるとテンプレートが表示されません

実践的なテンプレート例

1. シンプルなSwiftクラステンプレート

TemplateInfo.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Kind</key>
    <string>Xcode.IDEFoundation.TextSubstitutionFileTemplateKind</string>
    <key>Description</key>
    <string>A custom Swift class with protocol conformance options.</string>
    <key>Summary</key>
    <string>Custom Swift Class</string>
    <key>SortOrder</key>
    <string>1</string>
    <key>DefaultCompletionName</key>
    <string>CustomClass</string>
    <key>Platforms</key>
    <array>
        <string>com.apple.platform.iphoneos</string>
    </array>
    <key>Options</key>
    <array>
        <dict>
            <key>Identifier</key>
            <string>protocolConformance</string>
            <key>Name</key>
            <string>Protocol Conformance:</string>
            <key>Description</key>
            <string>Select protocols to conform to</string>
            <key>Type</key>
            <string>popup</string>
            <key>Values</key>
            <array>
                <string>None</string>
                <string>Codable</string>
                <string>ObservableObject</string>
                <string>Equatable</string>
            </array>
            <key>Default</key>
            <string>None</string>
        </dict>
    </array>
</dict>
</plist>

対応するSwiftファイル(___FILEBASENAME___.swift):

//___FILEHEADER___

import Foundation

class ___FILEBASENAMEASIDENTIFIER___: ___VARIABLE_protocolConformance___ {
    
    // MARK: - Properties
    
    // MARK: - Initialization
    
    init() {
        
    }
    
    // MARK: - Methods
    
}

2. 複雑なプロジェクトテンプレート例

TemplateInfo.plist(Widget Extension)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Kind</key>
    <string>Xcode.Xcode3.ProjectTemplateUnitKind</string>
    <key>Identifier</key>
    <string>com.custom.widgetExtension</string>
    <key>Ancestors</key>
    <array>
        <string>com.apple.dt.unit.coreDataCocoaTouchApplication</string>
    </array>
    <key>Concrete</key>
    <true/>
    <key>Description</key>
    <string>A widget extension with configurable Live Activity support.</string>
    <key>Options</key>
    <array>
        <dict>
            <key>Identifier</key>
            <string>includeALiveActivity</string>
            <key>Name</key>
            <string>Include Live Activity</string>
            <key>Description</key>
            <string>Include Live Activity implementation</string>
            <key>Type</key>
            <string>checkbox</string>
            <key>Default</key>
            <string>false</string>
        </dict>
        <dict>
            <key>Identifier</key>
            <string>includeAControl</string>
            <key>Name</key>
            <string>Include Control Widget</string>
            <key>Description</key>
            <string>Include Control Widget implementation</string>
            <key>Type</key>
            <string>checkbox</string>
            <key>Default</key>
            <string>false</string>
        </dict>
    </array>
    <key>Units</key>
    <dict>
        <key>true</key>
        <array>
            <dict>
                <key>RequiredOptions</key>
                <dict>
                    <key>includeALiveActivity</key>
                    <string>true</string>
                </dict>
                <key>Nodes</key>
                <array>
                    <string>LiveActivity.swift</string>
                    <string>LiveActivityBundle.swift</string>
                </array>
                <key>Definitions</key>
                <dict>
                    <key>LiveActivity.swift</key>
                    <dict>
                        <key>Path</key>
                        <string>LiveActivity.swift</string>
                    </dict>
                </dict>
            </dict>
            <dict>
                <key>RequiredOptions</key>
                <dict>
                    <key>includeAControl</key>
                    <string>true</string>
                </dict>
                <key>Nodes</key>
                <array>
                    <string>Control.swift</string>
                    <string>AppIntent.swift</string>
                </array>
            </dict>
        </array>
    </dict>
    <key>Targets</key>
    <array>
        <dict>
            <key>TargetIdentifier</key>
            <string>com.apple.dt.widgetExtension.iOS</string>
            <key>Name</key>
            <string>___PACKAGENAME___Extension</string>
            <key>Frameworks</key>
            <array>
                <string>WidgetKit</string>
                <string>SwiftUI</string>
            </array>
            <key>SharedSettings</key>
            <dict>
                <key>ENABLE_PREVIEWS</key>
                <string>YES</string>
                <key>IPHONEOS_DEPLOYMENT_TARGET</key>
                <string>16.0</string>
            </dict>
        </dict>
    </array>
    <key>AssociatedTargetSpecification</key>
    <dict>
        <key>PopUpTitle</key>
        <string>Embed in Application:</string>
        <key>AllowableProductTypes</key>
        <array>
            <string>com.apple.product-type.application</string>
        </array>
        <key>AssociatedTargetIsDependent</key>
        <true/>
    </dict>
</dict>
</plist>

高度な実装パターン

1. 継承を使った階層化テンプレート

基本テンプレート(BaseSwiftClass.xctemplate)を作成し、他のテンプレートで継承:

<key>Ancestors</key>
<array>
    <string>com.custom.baseSwiftClass</string>
</array>

2. 動的ファイル生成

選択されたオプションに応じて異なるファイルセットを生成:

<key>Units</key>
<dict>
    <key>CoreData</key>
    <array>
        <dict>
            <key>Nodes</key>
            <array>
                <string>DataModel.xcdatamodeld</string>
                <string>PersistenceController.swift</string>
            </array>
        </dict>
    </array>
    <key>UserDefaults</key>
    <array>
        <dict>
            <key>Nodes</key>
            <array>
                <string>UserDefaultsManager.swift</string>
            </array>
        </dict>
    </array>
</dict>

3. 条件付きビルド設定

選択したオプションに基づいてコンパイラフラグを設定:

<key>Targets</key>
<array>
    <dict>
        <key>RequiredOptions</key>
        <dict>
            <key>enableDebugLogging</key>
            <string>true</string>
        </dict>
        <key>SharedSettings</key>
        <dict>
            <key>SWIFT_ACTIVE_COMPILATION_CONDITIONS</key>
            <string>DEBUG_LOGGING</string>
        </dict>
    </dict>
</array>

ベストプラクティス

1. テンプレートの設計指針

  1. 再利用性を重視: 共通部分は基底テンプレートに分離
  2. 柔軟性を提供: Options を適切に活用してカスタマイズ可能に
  3. 保守性を考慮: 複雑になりすぎないよう適度に分割
  4. 一貫性を保持: 命名規則やファイル構成を統一

2. エラー処理とデバッグ

  1. plist構文チェック: XML形式が正しいかを確認
  2. 段階的テスト: 基本機能から順次複雑な機能を追加
  3. ログ活用: Xcodeのコンソール出力でエラー原因を特定
  4. バックアップ: 作業中のテンプレートは定期的にバックアップ

3. 配布と管理

  1. バージョン管理: Gitでテンプレートを管理
  2. ドキュメント化: 各テンプレートの使用方法を文書化
  3. チーム共有: 統一されたテンプレートをチーム全体で使用
  4. アップデート対応: Xcodeのバージョンアップ時の互換性確認

以上がXcodeテンプレートの作成方法に関する包括的なガイドです。このドキュメントを参考に、効率的な開発環境を構築してください。

Discussion