Importing Swift into Objective-C
Overview
You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file. This file is an Objective-C header that declares the Swift interfaces in your target, and you can think of it as an umbrella header for your Swift code. You don’t need to do anything special to create the generated header—just import it to use its contents in your Objective-C code.
The header's name is generated from your product module name, followed by "-Swift.h". By default, this name is the same as your product name, with any nonalphanumeric characters replaced with an underscore (_). If the name begins with a number, the first digit is replaced with an underscore.

The process for importing Swift declarations into Objective-C code differs slightly depending on whether you’re writing an app or a framework. Both processes are described below.
Import Code Within an App Target
When you're building an app target, you can import your Swift code into any Objective-C .m file within that same target using this syntax and substituting the appropriate name:
By default, the generated header contains interfaces for Swift declarations marked with the public or open modifier. If your app target has an Objective-C bridging header, the generated header also includes interfaces marked with the internal modifier. Declarations marked with the private or fileprivate modifier don't appear in the generated header, and aren't exposed to the Objective-C runtime unless they are explicitly marked with a @IBAction, @IBOutlet, or @objc attribute. Inside unit test targets, you can access imported internal declarations as if they were public by prepending @testable to the product module import statement.
The Swift interfaces in the generated header include references to all of the Objective-C types used in them, so make sure to import the Objective-C headers for those types first.
When building an app target, you can provide a custom name for the product module by changing the Product Module Name build setting. Xcode uses this name when naming the generated header file.
Import Code Within a Framework Target
To import a set of Swift files in the same framework target as your Objective-C code, import the Xcode-generated header for your Swift code into any Objective-C .m file where you want to use your Swift code.
Because the generated header is part of the framework’s public interface, only declarations marked with the public or open modifier appear in the generated header for a framework target. Methods and properties that are marked with the internal modifier and declared within a class that inherits from an Objective-C class are accessible to the Objective-C runtime. However, they're inaccessible at compile time and don't appear in the generated header for a framework target.
Import Swift code into Objective-C within the same framework:
Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.
Import the Swift code from that framework target into any Objective-C
.mfile within that target using this syntax and substituting the appropriate names:
Include Swift Classes in Objective-C Headers Using Forward Declarations
When declarations in an Objective-C header file refer to a Swift class or protocol that comes from the same target, importing the generated header creates a cyclical reference. To avoid this, use a forward declaration of the Swift class or protocol to reference it in an Objective-C interface.
Forward declarations of Swift classes and protocols can be used only as types for method and property declarations.
https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c
Importing Swift into Objective-C的更多相关文章
- IOS-Swift、Objective-C、C++混合编程
1.Objective-C调用C++代码 后缀为m文件的是Objective-C的执行文件,而后缀为mm文件的是Objective-C++文件. 直接在Objective-C中是无法调用C++代码的, ...
- Swift与Objective-C中的闭包
Swift Code: func makeIncrementor(forIncrement amount: Int) -> (() -> Int,() -> Int) { func ...
- Swift调用Objective C的FrameWork
很多Github的库经过很多年的发展,源码都是OC写的,,所以,用Swift调用OC的库就是开发中难免遇到的的一个问题,本文以AFNetworking为例,讲解如何跨语言调用. 第一步 创建一个空的工 ...
- Swift和Objective C关于字符串的一个小特性
一.Unicode的一个小特性 首先,Unicode规定了许多code point,每一个code point表示一个字符.如\u0033表示字符"3",\u864e表示字符&qu ...
- 在项目里交叉使用Swift和OC【转】
Swift and Objective-C in the Same Project在项目里交叉使用Swift和OC Swift与OC的兼容性使得你可以在项目里使用Swift+OC的方式编写应用程序,称 ...
- 在项目里交叉使用Swift和OC
Swift and Objective-C in the Same Project 在项目里交叉使用Swift和OC Swift与OC的兼容性使得你能够在项目里使用Swift+OC的方式编写应用程序, ...
- 开发 Swift 和 Objective-C 混编的 Framework
来源:黄文臣 blog.csdn.net/hello_hwc/article/details/58320433 前言 为什么要写这样一篇文章,因为昨天和一个朋友讨论到Swift和Objective C ...
- 使用命令行工具运行Xcode 7 UI Tests
原文:Run Xcode 7 UI Tests from the command line 苹果在Xcode 7中引入了一项新技术UI Tests,允许开发者使用Swift或Objective C代码 ...
- 手把手教你从Core Data迁移到Realm
来源:一缕殇流化隐半边冰霜 (@halfrost ) 链接:http://www.jianshu.com/p/d79b2b1bfa72 前言 看了这篇文章的标题,也许有些人还不知道Realm是什么,那 ...
随机推荐
- java 内存简介
java程序对内存分配的方式一般有三种: (1) 从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量. (2) 在栈上创建. 在执行函数时,函数内局 ...
- import-module in $profile
$PROFILE C:\Users\clu\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 Import-Module 'C:\Users\ ...
- gearcache在qemu-kvm虚拟化平台下的实现
需要用到的数据结构: 链表,基树. gearcache在qemu-kvm虚拟化平台下的实现主要有以下的步骤: 1.打开镜像文件的时候,为gearcache中的基数池(page_node_pool)和读 ...
- POJ3468:A Simple Problem with Integers (线段树||树状数组||Splay解决基本问题的效率对比)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- jsp中page指令用法详解
转自:https://www.jb51.net/article/73428.htm 一.JSP 指令 JSP 指令(directive)影响由 JSP 页面生成的 servlet 的整体结构.下面的模 ...
- Gym 100548A Built with Qinghuai and Ari Factor (水题)
题意:给定 n 个数,问是不是全是3的倍数. 析:略. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") ...
- 洛谷P3833 [SHOI2012]魔法树(树链剖分)
传送门 树剖板子…… 一个路径加和,线段树上打标记.一个子树询问,dfs的时候记录一下子树的区间就行 // luogu-judger-enable-o2 //minamoto #include< ...
- solr的安装配置与helloworld
一.安装solr 1.安装jdk环境和tomcat 2.解压solr压缩包,这里我解压到opt目录下 3.把/usr/local/solr-4.8.0/dist/solr-4.8.0.war部署到to ...
- JavaScript--DOM创建元素节点createElement
创建元素节点createElement createElement()方法可创建元素节点.此方法可返回一个 Element 对象. 语法: document.createElement(tagName ...
- 【CSS】少年,你想拥有写轮眼么?
最近笔者在公司内部开展了一次CSS讲座,由于授课经验不太足,授课效果自我感觉并不太好,不过课中有一个笔者用CSS写的一个小效果,其中还是包含了蛮多CSS的常见知识点的,正好也有部分同学很感兴趣如何实现 ...