ios 如何在 Swift 中导入现有的 Objective C 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24034409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:23:38  来源:igfitidea点击:

How to import existing Objective C classes in Swift

iosswift

提问by shah1988

I had been messing around with Swift for a while in XCode 6.0 DP to use it in my existing project. I am trying to access MyModel.h(My existing Objective C Model object) from my ViewController.swift file. I wanted to import

我在 XCode 6.0 DP 中使用 Swift 已经有一段时间了,以便在我现有的项目中使用它。我试图从我的 ViewController.swift 文件访问 MyModel.h(我现有的 Objective C 模型对象)。我想导入

#import "MyModel.h"to my Swift file. But I could not find how this can be done.

#import "MyModel.h"到我的 Swift 文件。但我找不到如何做到这一点。

回答by shah1988

Posting the answer if it helps some one facing the same issue.

如果可以帮助面临相同问题的人,请发布答案。

I found that a pretty straight forward solution for How to do this is given in the iOS Developer Library. Please refer to the following link:

我发现 iOS 开发人员库中提供了一个非常直接的解决方案如何做到这一点。请参考以下链接:

https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_75

https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_75

Apple Doc says:

苹果文档说:

To import a set of Objective-C files in the same app target as your Swift code, you rely on an Objective-C bridging header to expose those files to Swift. Xcode offers to create this header file when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app.

要在与 Swift 代码相同的应用程序目标中导入一组 Objective-C 文件,您需要依靠 Objective-C 桥接头将这些文件公开给 Swift。当您将 Swift 文件添加到现有的 Objective-C 应用程序或将 Objective-C 文件添加到现有的 Swift 应用程序时,Xcode 会提供创建此头文件的功能。

So I created MyApp-Bridging-Header.hfile and just added the following line:

所以我创建了MyApp-Bridging-Header.h文件并添加了以下行:

#import "MyModel.h"

Now it lets me use the model in my ViewController.swiftas follows:

现在它让我在我的模型中使用ViewController.swift如下:

var myModel = MyModel()
myModel.name = "My name"
myModel.dobString = "11 March,2013"
println ("my model values: Name: \myModel.name and dob: \myModel.dobString")

FYI to anyone who is trying to figure this out. If you have to create the bridging file from scratch, you also have to specify a path to it in Build Settings > Swift Compiler > Objective-C Bridging Header.

供任何试图解决此问题的人参考。如果您必须从头开始创建桥接文件,您还必须在 Build Settings > Swift Compiler > Objective-C Bridging Header 中指定一个路径。

回答by iPatel

In Document said in to swift programmingThere are no import statement.

文档中说到swift编程中没有import语句。

enter image description here

在此处输入图片说明

回答by Rohit

To import into Swift app or in Objective C app that is creating a mixed-language app you need to create a bridging header you can refer Apple Docs

要导入 Swift 应用程序或正在创建混合语言应用程序的 Objective C 应用程序,您需要创建一个桥接头,您可以参考Apple Docs

Its says

它说

Objective-C and Swift files can coexist in a single project, whether the project was originally an Objective-C or Swift project. You can simply add a file of the other language directly to an existing project. This natural workflow makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.

To import a set of Objective-C files in the same app target as your Swift code, you rely on an Objective-C bridging header to expose those files to Swift. Xcode offers to create this header file when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app.

Objective-C 和 Swift 文件可以共存于单个项目中,无论该项目最初是 Objective-C 还是 Swift 项目。您可以简单地将其他语言的文件直接添加到现有项目中。这种自然的工作流程使创建混合语言应用程序和框架目标与创建用单一语言编写的应用程序或框架目标一样简单。

要在与 Swift 代码相同的应用程序目标中导入一组 Objective-C 文件,您需要依靠 Objective-C 桥接头将这些文件公开给 Swift。当您将 Swift 文件添加到现有的 Objective-C 应用程序或将 Objective-C 文件添加到现有的 Swift 应用程序时,Xcode 会提供创建此头文件的功能。

EDIT: I have created a code for your help you can find it here

编辑:我已经创建了一个代码来帮助你,你可以在这里找到它