在Swift中,如何调用Objective-C代码?
苹果提到它们可以共存于一个应用程序中,但这是否意味着人们可以在技术上重用Objective-C中创建的旧类,同时在Swift中构建新类?
在Swift中,如何调用Objective-C代码?
苹果提到它们可以共存于一个应用程序中,但这是否意味着人们可以在技术上重用Objective-C中创建的旧类,同时在Swift中构建新类?
当前回答
点击新建文件菜单,并选择文件选择语言目标。这时,它会自动生成一个“Objective-C Bridging Header”文件,用于定义一些类名。
“Swift编译器-代码生成”下的“Objective-C桥接头”。
其他回答
以下是在Swift项目中使用Objective-C代码(在这种情况下,由第三方提供的框架)的逐步说明:
通过选择file -> New -> New file -> Objective-C file将任何Objective-C文件添加到Swift项目中。保存时,Xcode会询问你是否想要添加一个桥接头。选择“是”。 (来源:derrrick.com)
步骤简单:
A prompt appears, and then click on OK... If it does not appear, then we create it manually like in the following... Create one header file from iOS source and give the name ProjectName-Bridging-Header (example: Test-Bridging-Header), and then go to build setting in the Swift compiler code -> Objective-C bridge add Objective-C bridge name ..(Test/Test-Bridging-Header.h). Yeah, that's complete. Optionally, delete the Objective-C file you added (named "anything" in the GIF image above). You don't need it any more. Open the bridging header file -- the filename is of the form [YourProject]-Bridging-Header.h. It includes an Xcode-provided comment. Add a line of code for the Objective-C file you want to include, such as a third-party framework. For example, to add Mixpanel to your project, you will need to add the following line of code to the bridging header file: #import "Mixpanel.h" Now in any Swift file you can use existing Objective-C code, in the Swift syntax (in the case of this example, and you can call Mixpanel SDK methods, etc.). You need to familiarize yourself with how Xcode translates Objective-C to Swift. Apple's guide is a quick read. Or see this answer for an incomplete summary.
Mixpanel的示例:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Mixpanel.sharedInstanceWithToken("your-token")
return true
}
就是这样!
注意:如果你从你的项目中删除桥接头文件,请确保进入构建设置并在“Swift Compiler - Code Generation”下删除“Objective-C桥接头”的值。
我还想补充一点:
我非常感谢@Logan的回答。创建桥文件和设置有很大帮助。
但是在做了所有这些步骤之后,我仍然没有在Swift中获得Objective-C类。
我使用cocoapods库并将其集成到我的项目中。也就是豆荚"pop"。
因此,如果你在Swift中使用Objective-C pod,那么可能有一个机会,你不能获得或导入类到Swift中。
你要做的一件简单的事情是:
转到<YOUR-PROJECT>-Bridging-Header文件和 将语句#import <ObjC_Framework>替换为@import ObjC_Framework
例如:(Pop library)
取代
#import <pop/POP.h>
with
@import pop;
当#import无效时使用clang import。
苹果在这个文档中提供了官方指南: 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都是可见的。
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.
引用自文档:
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.