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

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.
主站蜘蛛池模板: 桑植县| 贵溪市| 上犹县| 崇左市| 东台市| 虎林市| 精河县| 招远市| 娄烦县| 巴南区| 天台县| 洪泽县| 连平县| 建平县| 丹江口市| 庄河市| 石泉县| 东港市| 舟山市| 淮滨县| 荣昌县| 崇左市| 类乌齐县| 黄骅市| 永仁县| 玛纳斯县| 双峰县| 剑川县| 易门县| 三穗县| 定边县| 越西县| 望城县| 青阳县| 乌拉特后旗| 织金县| 会东县| 汕尾市| 惠来县| 尼木县| 信丰县|