Article

Importing Swift into Objective-C SwiftをObjective-Cにインポートする

Access Swift types and declarations from within your Objective-C codebase. Swift型と宣言にあなたのObjective-Cコードベース内からアクセスします。

Overview 概要

You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file. This file is an Objective-C header that declares the Swift interfaces in your target, and you can think of it as an umbrella header for your Swift code. You don’t need to do anything special to create the generated header—just import it to use its contents in your Objective-C code. あなたは、Swiftにおいて宣言される型をあなたのプロジェクトの中のObjective-Cコード内から扱うことが、Xcode生成のヘッダファイルをインポートすることによって可能です。このファイルは、Swiftインターフェイスをあなたのターゲットにおいて宣言するObjective-Cヘッダです、そしてあなたはそれをあなたのSwiftコードのためのアンブレラファイルとみなせます。あなたは、生成ヘッダを作成するために特に何もする必要はありません ― あなたは、ただ単にそれをインポートしてそれの内容をあなたのObjective-Cコードにおいて使います。

The header's name is generated from your product module name, followed by "-Swift.h". By default, this name is the same as your product name, with any nonalphanumeric characters replaced with an underscore (_). If the name begins with a number, the first digit is replaced with an underscore. ヘッダの名前は、あなたの製品モジュール名から、それに"-Swift.h"を続けることで生成されます。省略時には、この名前は、あらゆる非アルファベット文字がアンダースコア(_)で置き換えられたあなたの製品名と同じです。名前が数字で始まるならば、最初の数字はアンダースコアで置き換えられます。

Diagram showing the steps to import Swift declarations into Objective-C code. Use forward declarations to declare Swift classes used in an Objective-C header file, and #import statements to import the Xcode-generated header into Objective-C .m files.

The process for importing Swift declarations into Objective-C code differs slightly depending on whether you’re writing an app or a framework. Both processes are described below. Swift宣言をObjective-Cコードにインポートする行程は、あなたがアプリを書いているかフレームワークを書いているかに従い、わずかに異なります。双方の行程は下で記述されます。

Import Code Within an App Target アプリターゲット内部でコードをインポートする

When you're building an app target, you can import your Swift code into any Objective-C .m file within that same target using this syntax and substituting the appropriate name: あなたがアプリターゲットをビルドしている場合、あなたはあなたのSwiftコードをインポートすることが、あらゆるObjective-C .mファイルへと、その同じターゲット内で、この構文を使ってそして適切な名前を代わりに使って、行えます。


#import "ProductModuleName-Swift.h"

By default, the generated header contains interfaces for Swift declarations marked with the public or open modifier. If your app target has an Objective-C bridging header, the generated header also includes interfaces marked with the internal modifier. Declarations marked with the private or fileprivate modifier don't appear in the generated header, and aren't exposed to the Objective-C runtime unless they are explicitly marked with a @IBAction, @IBOutlet, or @objc attribute. Inside unit test targets, you can access imported internal declarations as if they were public by prepending @testable to the product module import statement. 特に何もしなければ、生成されたヘッダはpublicまたはopen修飾子で印されたSwift宣言を含みます。あなたのアプリターゲットがObjective-Cブリッジヘッダを含むならば、生成されたヘッダもまたinternal修飾子で印されたインターフェイスを含みます。privateまたはfileprivate修飾子で印された宣言は生成されたヘッダに現れません、そしてObjective-Cランタイムに露出されません、それらが明示的に@IBAction@IBOutlet、または@objc属性で印されない限りは。ユニットテスト内部で、あなたはインポートされたinternal宣言に、まるでそれらがパブリックだったかのようにアクセスすることが、製品モジュールインポート文に@testableを前に付けることによって可能です。

The Swift interfaces in the generated header include references to all of the Objective-C types used in them, so make sure to import the Objective-C headers for those types first. 生成されたヘッダの中のSwiftインタフェースそれらは、それらの中で使われるObjective-C型への全ての参照を含みます、それで最初にそれらの型に対するObjective-Cヘッダをインポートすることを確実にしてください。

When building an app target, you can provide a custom name for the product module by changing the Product Module Name build setting. Xcode uses this name when naming the generated header file. アプリターゲットをビルドする場合、あなたはあつらえの名前を製品モジュールに提供することがProduct Module Name build設定を変更することによって可能です。Xcodeは、生成ヘッダファイルに名前を付けるときにこの名前を使います。

Import Code Within a Framework Target フレームワークターゲット内部でコードをインポートする

To import a set of Swift files in the same framework target as your Objective-C code, import the Xcode-generated header for your Swift code into any Objective-C .m file where you want to use your Swift code. 一揃いのSwiftファイルをあなたのObjective-Cコードと同じフレームワークターゲットの中でインポートするために、あなたのSwiftコードに対するXcode生成のヘッダを、そこにおいてあなたがあなたのSwiftコードを使うことを望む何らかのObjective-C .mファイルの中へとインポートしてください。

Because the generated header is part of the framework’s public interface, only declarations marked with the public or open modifier appear in the generated header for a framework target. Methods and properties that are marked with the internal modifier and declared within a class that inherits from an Objective-C class are accessible to the Objective-C runtime. However, they're inaccessible at compile time and don't appear in the generated header for a framework target. 生成されたヘッダがフレームワークのパブリックインターフェイスの一部であることから、publicまたはopen修飾子で印された宣言だけがフレームワークターゲットに対する生成ヘッダに現れます。internal修飾子で印されたそしてObjective-Cクラスから継承するクラス内部で宣言されたメソッドとプロパティは、Objective-Cランタイムからアクセス可能です。しかしながら、それらはコンパイル時にはアクセス不可能です、そしてフレームワークターゲットに対する生成ヘッダの中に現れません。

Import Swift code into Objective-C within the same framework: SwiftコードをObjective-Cへと同じフレームワーク内部でインポートする:

  1. Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes. 「Build Settings」の下、「Packaging」の中で、そのフレームワークターゲットのための「Defines Module」設定が「Yes」に設定されることを確認します。

  2. Import the Swift code from that framework target into any Objective-C .m file within that target using this syntax and substituting the appropriate names: そのフレームワークターゲットからのSwiftコードを何らかのObjective-C .mファイルにそのターゲット内部で、この構文を使ってそして適切な名前で置き換えて、インポートしてください:


#import <ProductName/ProductModuleName-Swift.h>

Include Swift Classes in Objective-C Headers Using Forward Declarations SwiftクラスをObjective-Cヘッダの中に前方宣言を使ってインクルードする

When declarations in an Objective-C header file refer to a Swift class or protocol that comes from the same target, importing the generated header creates a cyclical reference. To avoid this, use a forward declaration of the Swift class or protocol to reference it in an Objective-C interface. Objective-Cヘッダファイルの中の宣言がその同じターゲットからやってくるSwiftクラスまたはプロトコルを参照する場合、生成ヘッダのインポートは循環参照を生み出します。これを防ぐために、Swiftクラスまたはプロトコルの前方宣言を使うことで、それをObjective-Cインターフェイスにおいて参照してください。


// MyObjcClass.h
@class MySwiftClass;
@protocol MySwiftProtocol;


@interface MyObjcClass : NSObject
- (MySwiftClass *)returnSwiftClassInstance;
- (id <MySwiftProtocol>)returnInstanceAdoptingSwiftProtocol;
// ...
@end

Forward declarations of Swift classes and protocols can be used only as types for method and property declarations. Swiftのクラスとプロトコルの前方宣言は、メソッドおよびプロパティ宣言に対する型としてのみ使われることができます。

See Also 参照

Swift and Objective-C in the Same Project 同じプロジェクトの中のSwiftとObjective-C