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

  • 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.
主站蜘蛛池模板: 海南省| 永顺县| 奉贤区| 汨罗市| 和顺县| 勃利县| 靖西县| 古浪县| 天水市| 托克托县| 新宾| 崇仁县| 高雄市| 铜川市| 青岛市| 牙克石市| 扎赉特旗| 邹城市| 新平| 仙游县| 莱阳市| 孝义市| 诏安县| 南安市| 冀州市| 彩票| 静安区| 凌海市| 扶绥县| 白山市| 正定县| 镇原县| 墨玉县| 紫阳县| 永修县| 大关县| 东乌珠穆沁旗| 芷江| 石景山区| 云和县| 新民市|