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

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


当前回答

以下是在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桥接头”。

在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)
}

以下是在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的双向方法

1

在Xcode Project中创建bridge-header.h文件 在bridge-Header文件中导入.h文件 在Build设置中设置bridge-Header的路径。 清理项目

2

在项目中创建objective-c文件(它会自动在Build Settings中为您设置路径) 在bridge-Header文件中导入.h文件

现在可以开始了 谢谢

我写了一个简单的Xcode 6项目,展示了如何混合c++, Objective-C和Swift代码:

https://github.com/romitagl/shared/tree/master/C-ObjC-Swift/Performance_Console

特别地,这个例子调用了Swift中的Objective-C和c++函数。

关键是创建一个共享头文件Project-Bridging-Header.h,并将Objective-C头文件放在那里。

请下载该项目作为完整的示例。