官术网_书友最值得收藏!

Importing Objective-C in Swift

This is usually how we discover the interoperability layer. An existing Objective-C code base is getting upgraded to Swift and you need to expose existing Objective-C classes to your new Swift code. 

In Swift, all of your classes are available in the current module, depending on their access control scopes. In Objective-C, one developer need is to import a header through the #import "MyClass.h" directive or the module through @import ExternalLibrary. In order to expose your classes to Swift, you'll need to use a bridging header. Its responsibility is to expose only the classes you wish to the Swift compiler.

The bridging header is a header file that contains all of the import statements of the libraries and classes that are available to Swift. Once exposed this way, those classes are automatically available in all of the Swift code.

Let's set up a project for bridging:

  1. Add a header file to your project; call it Bridging-Header.h, for example
  2. In your Project Build settings, set SWIFT_OBJC_BRIDGING_HEADER to the path relative to your project root to your Bridging-Header.h file
  3. Add the required imports to your header
In step 2, you may need to double-check where your header file is located. If it's in a subfolder called  MyClasses, then the value should be MyClasses/Bridging-Header.h.
主站蜘蛛池模板: 南澳县| 德保县| 九台市| 水富县| 四平市| 通榆县| 汉中市| 沅江市| 哈巴河县| 邳州市| 洪泽县| 桂东县| 即墨市| 中宁县| 贺兰县| 邢台市| 修武县| 清流县| 延边| 南安市| 合水县| 高碑店市| 海原县| 安平县| 故城县| 万年县| 道真| 绿春县| 商都县| 儋州市| 瑞丽市| 贡嘎县| 雷波县| 吉安县| 叶城县| 珲春市| 乌兰察布市| 灵璧县| 泾源县| 秀山| 兖州市|