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

  • Hands-On Design Patterns with Swift
  • Florent Vilmart Giordano Scalzo Sergio De Simone
  • 220字
  • 2021-07-02 14:45:13

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.
主站蜘蛛池模板: 永登县| 迁西县| 宣城市| 葫芦岛市| 诸城市| 华宁县| 呼玛县| 女性| 宁波市| 淮北市| 绥德县| 家居| 抚宁县| 神池县| 通山县| 沙湾县| 灌南县| 日土县| 开平市| 兴义市| 缙云县| 咸宁市| 汉中市| 紫阳县| 疏附县| 酉阳| 调兵山市| 铅山县| 陵川县| 平山县| 昔阳县| 汶川县| 塔河县| 永德县| 陈巴尔虎旗| 高雄县| 屯昌县| 威宁| 巴塘县| 化德县| 邛崃市|