如何将 Swift 代码导入 Objective-C?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24102104/
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-09-03 21:18:30  来源:igfitidea点击:

How can I import Swift code to Objective-C?

objective-cimportswift

提问by Viktor Radchenko

I have written a library in Swift and I wasn't able to import it to my current project, written in Objective-C.

我用 Swift 编写了一个库,但无法将它导入到我当前用 Objective-C 编写的项目中。

Are there any ways to import it?

有什么办法可以导入吗?

#import "SCLAlertView.swift" - 'SCLAlertView.swift' file not found

回答by Bill

You need to import TargetName-Swift.h. Note that it's the target name - the other answers make the mistake of using the class name.

您需要导入TargetName-Swift.h. 请注意,这是目标名称 - 其他答案错误地使用了类名称。

This single file is an autogenerated header that defines Objective-C interfaces for all Swift classes in your project that are either annotated with @objcor inherit from NSObject.

这种单一的文件是一个自动生成的头,对项目中的所有雨燕类,要么与注释定义Objective-C接口@objc或继承NSObject

Considerations:

注意事项:

  • If your target name contains spaces, replace them with underscores (e.g. My Projectbecomes My_Project-Swift.h)

  • If your target is a framework, you need to import <TargetName/TargetName-Swift.h>

  • Make sure your Swift file is member of the target

  • 如果您的目标名称包含空格,请用下划线替换它们(例如My Project变成My_Project-Swift.h

  • 如果您的目标是框架,则需要导入 <TargetName/TargetName-Swift.h>

  • 确保您的 Swift 文件是目标的成员

回答by Andrei Papancea

Instructions from the Apple website:

来自苹果网站的说明:

To import Swift code into Objective-C from the same framework

Under Build Settings, in Packaging, make sure the Defines Modulesetting for that framework target is set to Yes. Import the Swift code from that framework target into any Objective-C .m file within that framework target using this syntax and substituting the appropriate names:

#import "ProductName-Swift.h"

将 Swift 代码从同一框架导入 Objective-C

生成设置,在包装,确保定义模块在该框架的目标设置为。使用以下语法将来自该框架目标的 Swift 代码导入到该框架目标内的任何 Objective-C .m 文件中,并替换适当的名称:

#import "ProductName-Swift.h"

Revision:

修订:

You can only import "ProductName-Swift.h"in .m files.

您只能在 .m 文件中导入“ProductName-Swift.h”

The Swift files in your target will be visible in Objective-C .m files containing this import statement.

To avoid cyclical references, don't import Swift into an Objective-C header file. Instead, you can forward declare a Swift class to use it in an Objective-C header. Note that you cannot subclass a Swift class in Objective-C.

目标中的 Swift 文件将在包含此导入语句的 Objective-C .m 文件中可见。

为了避免循环引用,不要将 Swift 导入到 Objective-C 头文件中。相反,您可以转发声明一个 Swift 类以在 Objective-C 标头中使用它。请注意,您不能在 Objective-C 中对 Swift 类进行子类化。

enter image description here

在此处输入图片说明

回答by Sumit singh

Here's what to do:

以下是该怎么做:

  1. Create a new Project in Objective-C

  2. Create a new .swiftfile ?

    • A popup window will appear and ask "Would You like to configure an Objective-C bridging Header".
    • Choose Yes.
  3. Click on your Xcode Project file

  4. Click on Build Settings

  5. Find the Search barand search for Defines Module.

  6. Change value to Yes.

  7. Search Product Module Name.

  8. Change the value to the name of your project.

  9. In App delegate, add the following : #import "YourProjectName-Swift.h"

  1. 在 Objective-C 中创建一个新项目

  2. 创建一个新.swift文件?

    • 将出现一个弹出窗口并询问“您是否想要配置一个 Objective-C 桥接头”
    • 选择
  3. 单击您的Xcode 项目文件

  4. 点击构建设置

  5. 找到搜索栏并搜索定义模块

  6. 将值更改为Yes

  7. 搜索产品模块名称

  8. 将值更改为您的项目名称。

  9. 在 App 委托中,添加以下内容: #import "YourProjectName-Swift.h"



Note:Whenever you want to use your Swift file you must be import following line :

注意:每当您想使用 Swift 文件时,您必须导入以下行:

#import "YourProjectName-Swift.h"

#import "YourProjectName-Swift.h"

回答by KingPolygon

If you're using Cocoapods and trying to use a Swift pod in an ObjC project you can simply do the following:

如果您正在使用 Cocoapods 并尝试在 ObjC 项目中使用 Swift pod,您可以简单地执行以下操作:

@import <FrameworkName>;

@import <FrameworkName>;

enter image description here

在此处输入图片说明

回答by Lukas Kalinski

Go to build settings in your project file and search for "Objective-C Generated Interface Header Name. The value of that property is the name that you should include.

转到项目文件中的构建设置并搜索“Objective-C Generated Interface Header Name该属性的值是您应该包含的名称。

If your "Product Module Name" property (the one that the above property depends on by default) varies depending on whether you compile for test/debug/release/etc (like it does in my case), then make this property independent of that variation by setting a custom name.

如果您的“产品模块名称”属性(上述属性默认依赖的属性)根据您是否为测试/调试/发布/等进行编译而有所不同(就像我的情况一样),则使该属性独立于该属性通过设置自定义名称来变化。

回答by Kiran Jasvanee

Importing Swiftfile inside Objective-ccan cause this error, if it doesn't importproperly.

如果不正确,导入Swift内部文件Objective-c可能会导致此错误import

NOTE: You don't have to import Swift files externally, you just have to import one file which takes care of swift files.

注意:您不必从外部导入 Swift 文件,您只需导入一个负责处理 Swift 文件的文件。

When you Created/Copied Swift fileinside Objective-C project. It would've created a bridging header automatically.

当您在Objective-C 项目中创建/复制 Swift 文件时。它会自动创建一个桥接头。

Check Objective-C Generated Interface Header Nameat Targets -> Build Settings.

检查Objective-C Generated Interface Header NameTargets -> Build Settings

enter image description here

在此处输入图片说明

Based on above, I will import KJExpandable-Swift.has it is.

基于上述,我将按KJExpandable-Swift.h原样导入。

Your's will be TargetName-Swift.h, Where TargetNamediffers based on your project name or another target your might have added and running on it.

您的 will TargetName-Swift.hTargetName根据您的项目名称或您可能已添加并在其上运行的其他目标而有所不同。

As below my target is KJExpandable, so it's KJExpandable-Swift.h
enter image description here

由于低于我的目标KJExpandable,所以它是KJExpandable-Swift.h
在此处输入图片说明

回答by Darshan Panchal

First Step:-

第一步:-

Select Project Target -> Build Setting -> Search('Define') -> Define Moduleupdate value Noto Yes

选择 Project Target -> Build Setting -> Search('Define') -> Define Moduleupdate value Noto Yes

"Defines Module": YES.

“定义模块”:

"Always Embed Swift Standard Libraries" : YES.

“始终嵌入 Swift 标准库”:是的

"Install Objective-C Compatibility Header" : YES.

“安装 Objective-C 兼容性头文件”:

enter image description here

在此处输入图片说明

Second Step:-

第二步:-

Add Swift file Class in Objective C ".h" File as below

在Objective C“.h”文件中添加Swift文件类如下

#import <UIKit/UIKit.h>

@class TestViewController(Swift File);

@interface TestViewController(Objective C File) : UIViewController

@end

Import 'ProjectName(Your Project Name)-Swift.h' in Objective C ".m" file

在Objective C“.m”文件中导入'ProjectName(Your Project Name)-Swift.h'

//TestViewController.m 
#import "TestViewController.h"

/*import ProjectName-Swift.h file to access Swift file here*/

#import "ProjectName-Swift.h"

回答by Misha Karpenko

There's one caveat if you're importing Swift code into your Objective-C files within the sameframework. You have to do it with specifying the framework name and angle brackets:

如果您将 Swift 代码导入同一框架内的Objective-C 文件,则需要注意一个问题。您必须通过指定框架名称和尖括号来完成:

#import <MyFramework/MyFramework-Swift.h>

#import <MyFramework/MyFramework-Swift.h>

MyFrameworkhere is the "Product Module Name" build setting (PRODUCT_NAME = MyFramework).

MyFramework这是“产品模块名称”构建设置 ( PRODUCT_NAME = MyFramework)。

Simply adding #import "MyFramework-Swift.h"won't work. If you check the built products directory (before such an #importis added, so you've had at least one successful build with some Swift code in the target), then you should still see the file MyFramework-Swift.hin the Headersdirectory.

简单地添加是#import "MyFramework-Swift.h"行不通的。如果您检查构建的产品目录(在#import添加此类之前,因此您至少已经成功构建了一个在目标中包含一些 Swift 代码的产品),那么您仍然应该MyFramework-Swift.hHeaders目录中看到该文件。

回答by Prashant Bhayani

If you have a project created in Swift 4 and then added Objective-C files, do it like this:

如果您在 Swift 4 中创建了一个项目,然后添加了 Objective-C 文件,请执行以下操作:

@objcMembers
public class MyModel: NSObject {
    var someFlag = false         
    func doSomething() {         
        print("doing something")
    }
}

Reference: https://useyourloaf.com/blog/objc-warnings-upgrading-to-swift-4/

参考:https: //useyourloaf.com/blog/objc-warnings-upgrading-to-swift-4/

回答by Misha Karpenko

Search for "Objective-C Generated Interface Header Name"in the Build Settings of the target you're trying to build (let's say it's MyApp-Swift.h), and import the value of this setting (#import "MyApp-Swift.h") in the source file where you're trying to access your Swift APIs.

在您尝试构建的目标的构建设置中搜索“Objective-C Generated Interface Header Name”(假设它是MyApp-Swift.h),然后#import "MyApp-Swift.h"在您尝试访问的源文件中导入此设置的值 ( )你的 Swift API。

The default value for this field is $(SWIFT_MODULE_NAME)-Swift.h. You can see it if you double-click in the value field of the "Objective-C Generated Interface Header Name" setting.

此字段的默认值为$(SWIFT_MODULE_NAME)-Swift.h。如果在“Objective-C Generated Interface Header Name”设置的值字段中双击就可以看到它。

Also, if you have dashes in your module name (let's say it's My-App), then in the $(SWIFT_MODULE_NAME)all dashes will be replaced with underscores. So then you'll have to add #import "My_App-Swift.h".

此外,如果您的模块名称中有破折号(假设它是My-App),那么在$(SWIFT_MODULE_NAME)所有破折号中都将替换为下划线。那么你必须添加#import "My_App-Swift.h".