46
44

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Objective-CプロジェクトをSwift化していく時のBridging-Headerの管理コストを下げる

Last updated at Posted at 2015-02-17

何ができるようになる?

Objective-CプロジェクトをSwiftに以降していく時に、都度更新する必要がある <#ProjectName#>-Bridging-Header.h (これ以降Bridging-Headerと書きます) を手作業で更新しなくて良くなります。

通常のBridging-Headerの管理

SwiftからObjective-Cのクラスを呼び出すには、Bridging-Header必要なクラスをimportします。

既存のObjective-CのプロジェクトをSwift化していく際には、最初にBridgin-Headerに多くのimport文を書くことになると思います。
さらにSwift化を進めていくとObjective-Cのファイルを削除する度に、Bridging-Headerを更新しなくてはいけません。

そこで、Objective-Cファイルを削除しても、Bridging-Headerを手作業で更新する必要がないような方法を紹介します。

手順

RunScriptを追加

BuildPhaseにBridging-Headerを更新するRunScriptを追加します。

Xcode→プロジェクトファイル→TARGETS→Build Phases→左上の「+」ボタン→New Run Script Phase
追加したRun Scriptは並べ替える事ができます。 Compile Sourcesより前にこのScriptを配置してください。

echo '#import "AppDelegate.h"' > ./TestProject/TestProject-Bridging-Header.h
find ./TestProject/Classes -type f -name "*.h" -exec basename {} \; | sed 's/^\(.*\)$/#import "\1"/' >> ./TestProject/TestProject-Bridging-Header.h

シェルでBridging-Headerにimport文を挿入しています。
このスクリプトは各プロジェクトのファイル配置により適宜変更してください。

↓こんな感じになります。
update_bridging_header.png
ここではRun Script(1)の名前を Update Bridging-Header.hに変更しています。

仕組み

  • Build PhaseのRunScriptで、ビルド時のコンパイル前にBridging-Headerを更新します。

まとめ

Build PhaseのRun Scriptを使えば、Bridging-Headerの手作業による更新がなくなります。
Swift化の煩わしい作業をなくしましょう。

46
44
1

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
46
44

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?