在Swift中,如何调用Objective-C代码?

苹果提到它们可以共存于一个应用程序中,但这是否意味着人们可以在技术上重用Objective-C中创建的旧类,同时在Swift中构建新类?


当前回答

创建桥接头后,进入构建设置=>搜索“Objective-C桥接头”。

就在下面你会发现“Objective-C Generated Interface Header Name”文件。

在视图控制器中导入那个文件。

例子:在我的例子中:“Dauble-Swift.h”

其他回答

参见Apple的使用Cocoa和Objective-C的Swift指南。本指南涵盖了如何使用Swift中的Objective-C和C代码,反之亦然,并对如何转换项目或在现有项目中混合和匹配Objective-C/C和Swift部分提出了建议。

编译器自动生成调用C函数和Objective-C方法的Swift语法。如文档所示,这个Objective-C:

UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

变成了这样的Swift代码:

let myTableView: UITableView = UITableView(frame: CGRectZero, style: .Grouped)

Xcode也可以在运行中进行这种转换——你可以在编辑Swift文件时使用Open quick,并输入Objective-C类名,它会带你到Swift化的类头。(你也可以通过cmd点击Swift文件中的API符号来获得。)iOS 8和OS X v10.10 (Yosemite)开发者库中的所有API参考文档在Objective-C和Swift表单(例如UIView)中都是可见的。

引用自文档:

Any Objective-C framework (or C library) that’s accessible as a module can be imported directly into Swift. This includes all of the Objective-C system frameworks—such as Foundation, UIKit, and SpriteKit—as well as common C libraries supplied with the system. For example, to import Foundation, simply add this import statement to the top of the Swift file you’re working in: import Foundation This import makes all of the Foundation APIs—including NSDate, NSURL, NSMutableData, and all of their methods, properties, and categories—directly available in Swift.

Create a .h file from NewFile -> Source -> header file Then save the name of file Your_Target_Name-Bridging-Header.h People here gets common mistake by taking their project name but it should be the Project's Target's name if in case both are different, generally they are same. Then in build settings search for Objective-C Bridging Header flag and put the address of your newly created bridging file, you can do it right click on the file -> show in finder -> drag the file in the text area then the address will be populated. Using #import Your_Objective-C_file.h In the swift file you can access the ObjC file but in swift language only.

混合Objective-C和Swift

高层图:

相同的目标

//Objective-C exposes API for Swift thought <target_name>-Bridging-Header.h
.swift uses .h.m = Swift consumer, Objective-C producer = <target_name>-Bridging-Header.h

//Swift exposes API for Objective-C thought <target_name>-Swift.h
.h.m uses .swift = Objective-C consumer, Swift producer = <target_name>-Swift.h

Swift消费者,Objective-C生产者

添加一个新的头文件。h和实现文件。m - Cocoa类文件(Objective-C) 例如<MyFileName>.h和<MyFileName>.m 配置桥接报头 当您看到“是否想配置Objective-C桥接头”时,单击“是” <target_name>-Bridging-Header.h将自动生成 构建设置-> Objective-C桥接头(SWIFT_OBJC_BRIDGING_HEADER) 将类添加到桥接头 在<target_name>-Bridging-Header.h中添加一行#import "<MyFileName>.h"

之后你可以使用Swift Objective-C的MyFileName.h,MyFileName.m

附注:如果你要添加一个现有的Objective-C文件到Swift项目中,请预先添加Bridging-Header.h并导入它

Objective-C消费者,Swift生产者

添加一个<MyFileName>.swift和extends NSObject 将Swift文件导入ObjC类 在Objective-C文件中添加#import "<target_name>-Swift.h" 通过@objc公开Swift代码[关于]

在此之后,您可以使用Objective-C Swift的<MyFileName>.swift

[<product_name>-Swift.h文件未找到]

不同的目标 (。modulemap]和[伞盖.h]用于暴露Objective-C for Swift。Swift.h用于为Objective-C公开Swift代码

(词汇)

给那些试图将Objective-C库添加到Swift的人一个提示:你应该在构建设置中添加- objc ->链接->其他链接器标志。