2019-10-09 如何在Swift中使用OC

声明OC的头文件在Swift中使用实际上分为两种情况:

一种是对于一个应用本身(App Target),我们需要声明 Objective-C Bridging Header

  1. In your Objective-C bridging header, import every Objective-C header you want to expose to Swift.
  2. In Build Settings, in Swift Compiler - Code Generation, make sure the Objective-C Bridging Header build setting has a path to the bridging header file. The path should be relative to your project, similar to the way your Info.plist path is specified in Build Settings. In most cases, you won't need to modify this setting.


另一种是作为一个静态库(Framework Target),我们需要使用 Umbrella Header,这样所有的Swift文件就会自动识别所有被声明的 OC 头文件,并且Swift文件不需要再次引入声明 "import statements"

  1. Under Build Settings, in Packaging, make sure the Defines Module setting for the framework target is set to Yes.


  2. In the umbrella header, import every Objective-C header you want to expose to Swift.

更多详细的信息可以参考 Importing Objective-C into Swift

推荐阅读更多精彩内容