在Swift中,如何调用Objective-C代码?
苹果提到它们可以共存于一个应用程序中,但这是否意味着人们可以在技术上重用Objective-C中创建的旧类,同时在Swift中构建新类?
在Swift中,如何调用Objective-C代码?
苹果提到它们可以共存于一个应用程序中,但这是否意味着人们可以在技术上重用Objective-C中创建的旧类,同时在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.
其他回答
在Xcode 10.1中的Swift 4.2.1项目中,您可以轻松地添加Objective-C文件。按照以下步骤将Objective-C文件连接到Swift项目。
Step_01:使用Swift语言创建新的Xcode项目:
文件>新建>项目> objc。
在Swift项目中添加新的Objective-C文件:
File >新建>文件…> macOS > Objective-C文件。
Step_03:如果你第一次添加一个新的Objective-C文件到Swift项目中,Xcode会问你:
你想配置一个Objective-C桥接头吗?
Step_04:选择选项:
创建桥接头。
Step_05:将生成一个对应的文件,文件的名称如下:
Objc-Bridging-Header.h。
Step_06:现在,你需要在桥标头中设置桥文件路径。在项目导航器中单击名称为objc的项目,然后选择:
> Objective-C桥接头> objc桥接头。h
将Objc-Bridging-Header.h拖放到该框中生成文件路径。
打开你的Objc-Bridging-Header.h文件,并导入你想要在Swift文件中使用的Objective-C文件。
#import "SpecialObjcFile.m"
下面是SpecialObjcFile.m的内容:
#import <Foundation/Foundation.h>
@interface Person: NSObject {
@public
bool busy;
}
@property
bool busy;
@end
现在在你的Swift文件中,你可以使用一个Objective-C类:
override func viewDidLoad() {
super.viewDidLoad()
let myObjcContent = Person()
print(myObjcContent.busy)
}
参见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)中都是可见的。
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.
苹果在这个文档中提供了官方指南: how-to-call-objective-c-code-from-swift
以下是相关部分:
要将一组Objective-C文件导入到同一个应用程序目标中的Swift代码中,您需要依赖Objective-C桥接头文件向Swift公开这些文件。当你将一个Swift文件添加到一个现有的Objective-C应用程序中,或者将一个Objective-C文件添加到一个现有的Swift应用程序中时,Xcode提供了创建这个头文件的功能。
如果你接受,Xcode会创建桥接头文件和你创建的文件一起,并使用你的产品模块名后跟"-Bridging-Header.h"来命名它。或者,你也可以自己创建一个桥接头,方法是选择文件>新建>文件>[操作系统]>源>头文件
编辑桥接头,将Objective-C代码暴露给Swift代码:
在你的Objective-C桥接头中,导入每个你想要向Swift公开的Objective-C头。 在构建设置中,在Swift Compiler - Code Generation中,确保Objective-C桥接头构建设置具有桥接头文件的路径。路径应该是相对于你的项目,类似于你的信息的方式。plist路径在Build Settings中指定。在大多数情况下,您不需要修改此设置。
在桥接头中列出的任何公共Objective-C头对Swift都是可见的。
点击新建文件菜单,并选择文件选择语言目标。这时,它会自动生成一个“Objective-C Bridging Header”文件,用于定义一些类名。
“Swift编译器-代码生成”下的“Objective-C桥接头”。