LoginSignup
4
6

More than 5 years have passed since last update.

Xcodeの新規作成時のファイルテンプレートをカスタマイズする

Posted at

概要

UIViewController等を新規作成する際に、不要なコードが入っている。また、チーム内のコーディングルールに則ったカスタムのテンプレートを利用したい。そこで、Xcodeインストール時に用意されたテンプレートをカスタマイズし、ファイルの新規作成時に読み込めるようにする。

インストール方法と使い方

1. シェルスクリプトとサンプルテンプレートの入手

Xcode-Template@github よりxcodeTemplateディレクトリにあるシェルスクリプトとサンプルテンプレートをダウンロードする。

2. テンプレートの配備

シェルスクリプトにより、カスタムテンプレートをXcodeで読み込めるようにする。Xcodeを開いている場合には、再起動する。

xcodeTemplate> chmod +x deploy_templates.sh
xcodeTemplate> ./deploy_templates.sh

3. テンプレートの選択

ファイルの新規作成より、[Custom]-[Cocoa Touch Class]を選択

select_custom

4. クラスの作成

カスタムしたCocoa Tocuhクラスのサブクラスとしてクラスを作成する。(例ではUIViewController)

new_file

5. テンプレートの適用を確認

custom_view_controller

テンプレートの修正方法

xcodeTemplate/Cocoa Touch Class.xctemplate/UIViewControllerSwift/___FILEBASENAME___.swiftを修正する。
また、修正後には、再度シェルスクリプトを起動する。

UIViewController以外を修正する場合には、/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source/Cocoa Touch Class.xctemplate にある修正したいクラス名のディレクトリをxcodeTemplate/Cocoa Touch Class.xctemplate以下にコピーし、修正する。

サンプル

Xcode-Template@githubに動作するプロジェクトがあります。

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6