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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,196评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,576评论 0 23
  • Swift 已推出数年,与Objective-C相比Swift的语言机制及使用简易程度上更接地气,也大大降低了iO...
    shuigenObba阅读 3,222评论 0 0
  • and you? “不熬夜,是一个人最好的自律。” —— 晚安
    步青墨阅读 1,409评论 0 0
  • 我的同事里有个顶顶不错的女孩,她的名字叫杜艳。我们是在培优学校,我教语文,她教美术。 她来应聘时,给大家上了一堂生...
    苍须阅读 2,720评论 0 10

友情链接更多精彩内容