在Swift中,如何调用Objective-C代码?
苹果提到它们可以共存于一个应用程序中,但这是否意味着人们可以在技术上重用Objective-C中创建的旧类,同时在Swift中构建新类?
在Swift中,如何调用Objective-C代码?
苹果提到它们可以共存于一个应用程序中,但这是否意味着人们可以在技术上重用Objective-C中创建的旧类,同时在Swift中构建新类?
当前回答
使用objective-c的双向方法
1
在Xcode Project中创建bridge-header.h文件 在bridge-Header文件中导入.h文件 在Build设置中设置bridge-Header的路径。 清理项目
2
在项目中创建objective-c文件(它会自动在Build Settings中为您设置路径) 在bridge-Header文件中导入.h文件
现在可以开始了 谢谢
其他回答
以下是在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桥接头”的值。
点击新建文件菜单,并选择文件选择语言目标。这时,它会自动生成一个“Objective-C Bridging Header”文件,用于定义一些类名。
“Swift编译器-代码生成”下的“Objective-C桥接头”。
你可以看看Swift & Cocoapods的帖子。基本上,我们需要创建一个桥接头文件,并将所有Objective-C头文件放在那里。然后我们需要从构建设置中引用它。之后,我们可以使用Objective-C代码。
let manager = AFHTTPRequestOperationManager()
manager.GET(
"http://example.com/resources.json",
parameters: nil,
success: { (operation: AFHTTPRequestOperation!,
responseObject: AnyObject!) in
println("JSON: " + responseObject.description)
},
failure: { (operation: AFHTTPRequestOperation!,
error: NSError!) in
println("Error: " + error.localizedDescription)
})
也可以看看苹果的文档Using Swift with Cocoa和Objective-C。
给那些试图将Objective-C库添加到Swift的人一个提示:你应该在构建设置中添加- objc ->链接->其他链接器标志。
混合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代码
(词汇)