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:

  1. Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.
  2. Import the Swift code from that framework target into any Objective-C .m file 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

原文地址:https://www.cnblogs.com/feng9exe/p/9712318.html

时间: 2024-08-30 16:18:18

Importing Swift into Objective-C的相关文章

IOS-Swift、Objective-C、C++混合编程

1.Objective-C调用C++代码 后缀为m文件的是Objective-C的执行文件,而后缀为mm文件的是Objective-C++文件. 直接在Objective-C中是无法调用C++代码的,所以如果需要在Objective-C调用C++语言就需要直接将后缀m文件改为mm,然后就可以调用C++代码了. Objective-C兼容C,Objective-C++兼容C.C++. 接下来是在OC工程中创建C++文件,并调用C++的代码: 然后在OC文件中直接用C++的语法调用C++,所以前提是

Swift调用Objective C的FrameWork

很多Github的库经过很多年的发展,源码都是OC写的,,所以,用Swift调用OC的库就是开发中难免遇到的的一个问题,本文以AFNetworking为例,讲解如何跨语言调用. 第一步 创建一个空的工程  注意,语言选择Swift 第二步,创建bridging-header 这个文件的作用,就是把OC的接口暴露给Swift.有两种方式  方式一  创建一个OC文件,然后点击确定    弹出是否要创建Bridging-Header,点击确定,这样会生成三个文件,其中的一个就是我们要用的SwiftU

Swift中的错误处理

前言 任何代码都会发生错误,这些错误有些是可以补救的,有些则只能让程序崩溃.良好的错误处理能够让你的代码健壮性提高,提高程序的稳定性. 本文的Swift版本:Swift 3 Objective C 返回nil 如果出错了,就返回空是Objective C中的一种常见的处理方式.因为在Objective C中,向nil发送消息是安全的.比如: - (instancetype)init { self = [super init]; if (self) { } //如果初始化失败,会返回nil ret

在项目里交叉使用Swift和OC

Swift and Objective-C in the Same Project 在项目里交叉使用Swift和OC Swift与OC的兼容性使得你可以在项目里使用Swift+OC的方式编写应用程序,称为混合+匹配(mix and match),用这种方式可以一边使用享受新语言的快速特性,一边无缝的接合你已有的OC代码(原文听起来好NB的样子,其实也就那么回事),不管项目最初是用OC编写的还是新建的Swift项目,都可以在这个项目里用OC和Swift混合继续开发 Importing Object

在项目里交叉使用Swift和OC【转】

Swift and Objective-C in the Same Project在项目里交叉使用Swift和OC Swift与OC的兼容性使得你可以在项目里使用Swift+OC的方式编写应用程序,称为混合+匹配(mix and match),用这种方式可以一边使用享受新语言的快速特性,一边无缝的接合你已有的OC代码(原文听起来好NB的样子,其实也就那么回事),不管项目最初是用OC编写的还是新建的Swift项目,都可以在这个项目里用OC和Swift混合继续开发 Importing Objecti

寒城攻略:Listo 教你 25 天学会 Swift 语言 - 25 Listo's Conclusion

import Foundation //*********************************************************************************************** //1.Listo's Conclusion(Listo 的编写总结) //_______________________________________________________________________________________________

Swift和OC混合使用

Swift and Objective-C in the Same Project在项目里交叉使用Swift和OC Swift与OC的兼容性使得你可以在项目里使用Swift+OC的方式编写应用程序,称为混合+匹配(mix and match),用这种方式可以一边使用享受新语言的快速特性,一边无缝的接合你已有的OC代码(原文听起来好NB的样子,其实也就那么回事),不管项目最初是用OC编写的还是新建的Swift项目,都可以在这个项目里用OC和Swift混合继续开发 Importing Objecti

使用命令行工具运行Xcode 7 UI Tests

原文:Run Xcode 7 UI Tests from the command line 苹果在Xcode 7中引入了一项新技术UI Tests,允许开发者使用Swift或Objective C代码来进行UI测试.直到现在一个可行的方法是使用UI Automation.通过UI Tests,可使用Xcode适当调试问题以及使用Swift或Objective C而无需处理JavaScript代码.首先为UI Tests创建一个新的target: 在测试分区下,选中Cocoa Touch UI T

手把手教你从Core Data迁移到Realm

来源:一缕殇流化隐半边冰霜 (@halfrost ) 链接:http://www.jianshu.com/p/d79b2b1bfa72 前言 看了这篇文章的标题,也许有些人还不知道Realm是什么,那么我先简单介绍一下这个新生的数据库.号称是用来替代SQLite 和 Core Data的.Realm有以下优点: 使用方便 Realm并不是基于SQLite的对象关系映射数据库.它是使用自己的持久化引擎,为简单和速度而生.用户们说,他们在数分钟之内就上手了Realm,构建一个app只需要数小时,每个