Overview

You can use Objective-C and Swift files together in a single project, no matter which language the project used originally. This makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.

The process for using your Objective-C declarations from your Swift code within mixed-language targets differs slightly depending on whether you’re writing an app or a framework. Both processes are described below.

Import Code Within an App Target

To import a set of Objective-C files into Swift code within the same app target, you rely on an Objective-C bridging header file to expose those files to Swift. Xcode offers to create this header when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app.

If you accept, Xcode creates the bridging header file along with the file you were creating, and names it by using your product module name followed by "-Bridging-Header.h". Alternatively, you can create a bridging header yourself by choosing File > New > File > [operating system] > Source > Header File.

Edit the bridging header to expose your Objective-C code to your Swift code:

  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.

Any public Objective-C headers listed in the bridging header are visible to Swift. The Objective-C declarations are automatically available from any Swift file within that target, with no import statements. Use classes and other declarations from your custom Objective-C code with the same Swift syntax you use for system classes.

Import Code Within a Framework Target

To use the Objective-C declarations in files in the same framework target as your Swift code, you’ll need to import those files into the Objective-C umbrella header—the master header for your framework. Import your Objective-C files by configuring the umbrella header:

  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.

Swift sees every header you expose publicly in your umbrella header. The contents of the Objective-C files in that framework are automatically available from any Swift file within that framework target, with no import statements. Use classes and other declarations from your Objective-C code with the same Swift syntax you use for system classes.

https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_objective-c_into_swift

Importing Objective-C into Swift的更多相关文章

  1. 【转载】谷歌酝酿将苹果Swift作为安卓APP主要开发语言

    TNW中文站 4月8日报道 安卓操作系统的软件开发语言是Java,而在过去几年中,有关Java的版权,谷歌(微博)和甲骨文之间发生了长期的诉讼.最新外媒消息称,谷歌正在考虑将苹果开发的Swift作为未 ...

  2. Object-C与Swift混合开发

    Object-C作为Apple的iOS App开发语言服务了很多个年头,2014年Apple推出了新的编程语言Swift.更高效更安全的口号再次吸引了一大批非iOS开发程序猿进入,小编觉得Swift代 ...

  3. 2016年iOS技术圈回顾

    2016年同2015年一样,在我还没有做好心理准备的时候,一晃神就到了年底.年关将近,不知诸君心情如何,年初的规划实现了多少,来年的计划又是否已有了眉目.年过三十的Peak君感觉年关是越来越难过了,越 ...

  4. fir.im Weekly - 这是一份强大的 SwiftGuide

    大新闻!Apple 10 亿美元融资滴滴!库克大叔对中国 iOS 开发者表达了高度认可,同时也传出 iOS 10 将内置滴滴 App 的消息.想像下,某个加班的深夜飙完代码,最性感的事情莫过于:「Si ...

  5. 设置app的状态栏样式

    http://www.jianshu.com/p/9f7f3fa624e7 http://cocoa.venj.me/blog/view-controller-based-status-bar-sty ...

  6. 转: app端数据库(性能高) realm (ios, android 均支持)

    转:  http://ios.jobbole.com/85041/ 移动端数据库新王者:realm 2016/05/14 · iOS开发 · 数据库 分享到:0 原文出处: 没故事的卓同学(@没故事的 ...

  7. realm-java 源码疑问

    JNIEXPORT void JNICALL Java_io_realm_internal_Group_nativeWriteToFile( JNIEnv* env, jobject, jlong n ...

  8. 《JavaScript权威指南》学习——js闭包

    序:闭包这个玩意啊~在很多没有代码块的语言中都会出现,已经成为大多程序员入门的一道坎,闭包让很多程序员觉得晦涩(事实上百度一下这个名词,真的说的很晦涩啊亲==|||),我第一次知道闭包这个名词是从&l ...

  9. 使用Xamarin实现跨平台移动应用开发(转载)

    刚在朋友圈看到张善友,转发的一条分享“使用Xamarin实现跨平台移动应用开发”,写的确实很详细得体,从收费到开源,这段时间xamarin受到不少质疑,如此文http://blog.csdn.net/ ...

  10. Android数据库Realm实践

    Android开发中常用的数据库有5个: 1. OrmLite OrmLite 不是 Android 平台专用的ORM框架,它是Java ORM.支持JDBC连接,Spring以及Android平台. ...

随机推荐

  1. Buildroot构建指南——根文件系统(Rootfs)【转】

    本文转载自; 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   Buildroot构建指南——根文件系统(Rootfs) Buildroot的Rootfs构建流程有一个大 ...

  2. 洛谷P3778 [APIO2017]商旅——01分数规划

    题目:https://www.luogu.org/problemnew/show/P3778 转化有点技巧: 其实直接关注比率的上下两项,也就是盈利和时间: 通过暴枚和 floyd 可以处理出两两点间 ...

  3. virtualbox安装完系统之后重新启动重新进入安装界面原因

    安装完成之后如果重新启动出现卡死的情况,直接关闭掉virtualbox,然后重新启动. 在这里记得完成安装后再改下设置,把Storage里的IDE控制器设为没有盘片,否则下次启动时又是安装界面.

  4. 【转载】Nginx 的工作原理 和优化

    1. Nginx的模块与工作原理 Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(locati ...

  5. Ubuntu 12.04下安装配置体验GNOME 3(转载)

    转自:http://www.tuicool.com/articles/zIbeIj 自己并不是一个思想前卫的人,穿衣审美也都是大众眼光.但是唯独喜欢在计算机方便尝试最新,心肝情愿的做小白鼠.近日,按耐 ...

  6. adb: command not found 解決方法(转载)

    转自:http://a7419.pixnet.net/blog/post/59806205-adb%3A-command-not-found--%E8%A7%A3%E6%B1%BA%E6%96%B9% ...

  7. Ruby  Hash类

    Hash类 更新:2017/06/15 获取没有的哈希值时返回nil 更新:2018/01/03 增加merge! 更新: 2018/04/05 增加搜索 key 更新: 2018/04/30 增加e ...

  8. bzoj 4827: [Hnoi2017]礼物【FFT】

    记得FFT要开大数组!!开到快MLE的那种!!我这个就是例子TAT,5e5都RE了 在这题上花的时间太多了,还是FFT不太熟练. 首先看70分的n方做法:从0下标开始存,先n--,把a数组倍增,然后枚 ...

  9. P4324 [JSOI2016]扭动的回文串

    传送门 对\(A\).\(B\)串各跑一遍\(manacher\),求出第\(1\).\(2\)类扭动回文串的最大长度. 考虑第三类的扭动回文串\(S(i,j,k)\),一定可以表示为\(A(i,l) ...

  10. springboot(七) 配置嵌入式Servlet容器

    github代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service ...