LoginSignup
27

More than 5 years have passed since last update.

既存プロジェクトにSwiftでExtensionを作ったらなんか動かない時の対処法

Posted at

iOS8GM出ましたね!いまiOS8対応真っ盛りだと思います。

ObjCのアプリでSwiftのExtensionを作るとクラッシュ

iOS8でExtensionが追加され挑戦されている方が多いかと思います。
そしてターゲットがわかれているのであえてSwiftに挑戦されている方もいるでしょう。ここで罠がありましたのでお知らせします。ObjCのアプリでSwiftのExtensionを作るとクラッシュします!

原因

Swiftを実行するにはSwift Standard Libraryが含まれている必要がありますが、ObjCのプロジェクトにはこれは含まれません。
そしてExtensionなどはそれ自身ではStandard Libraryを持っておらず、埋め込んだアプリのStandard Libraryを利用する仕様となっています。
したがって、ObjCのプロジェクトに対してSwiftでExtensionを作るとStandard Libraryが無いので読み込めずExtensionが即落ちして起動しなくなります。

対処法

メインのアプリの方にSwift Standard Libraryを入れる必要があります。
対応方法はメインアプリの方のBuild SettingsでEmbedded Content Contains Swift CodeをYESに設定します。
スクリーンショット 2014-09-10 14.35.15.png

あとなCleanして本体アプリをビルドした後にExtensionをビルドすれば動くようになるはずです。

ソース

Xcode6のリリースノートにKnown Issueで書いていました。
Xcode6.1までに直っていることを期待したいです。

The build step which embeds the Swift standard libraries in a bundle only runs for application product types, and only if the application itself, independent of any embedded content, contains Swift source files.
When building an application that does not contain Swift source files but embeds other content (like frameworks, XPC services, app extensions, and so on) that does contain Swift code, you must set the build setting Embedded Content Contains Swift Code (EMBEDDED_CONTENT_CONTAINS_SWIFT). That way the Swift libraries will be included in the application. (17757566)
http://adcdownload.apple.com//Developer_Tools/xcode_6_gm_seed_txl4k3/xcode_6_release_notes.pdf

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
27