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

时间:2023-01-23 18:39:49

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

11 个解决方案

#1


299  

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 @objc or inherit from NSObject.

这个单独的文件是一个自动生成的标头,它为您的项目中所有的Swift类定义了Objective-C接口,这些接口要么用@objc注释,要么从NSObject继承。

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

如果您的目标名称包含空格,请用下划线替换它们(例如:“我的项目”成为“My_Project-Swift.h”)

#2


103  

Instructions from the Apple website:

来自苹果官网的说明:

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

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

Under Build Settings, in Packaging, make sure the Defines Module setting 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:

在Build Settings下,在打包时,确保为该框架目标定义的模块设置为Yes。将该框架目标中的Swift代码导入到该框架目标中的任何Objective-C .m文件中,使用此语法并替换合适的名称:

#import "ProductName-Swift.h"

#进口“ProductName-Swift.h”

Revision:

修订:

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

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

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

在Objective-C .m文件中,您的目标中的Swift文件将会可见,其中包含了这个import语句。

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头文件中。相反,您可以转发声明一个Swift类以在Objective-C header中使用它。注意,在Objective-C中不能子类化一个Swift类。

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

#3


95  

Here's what to do :

以下是该做的:

  1. Create a new Project in Objective-C

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

  2. Create a new .swift file  

    创建一个新的.swift文件。

    • A popup window will appear and ask "Would You like to configure an Objective-C bridging Header".
    • 弹出窗口将出现并询问“您是否想配置一个Objective-C连接头”。
    • Choose Yes.
    • 选择Yes。
  3. Click on your Xcode Project file

    单击您的Xcode项目文件。

  4. Click on Build Settings

    点击建立设置

  5. Find the Search bar and search for Defines Module.

    找到搜索栏并搜索定义模块。

  6. Change value to Yes.

    变化值为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"

    在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"

#进口“YourProjectName-swift.h”

#4


23  

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

如果你正在使用Cocoapods并试图在ObjC项目中使用一个快速pod,你可以简单地做以下工作:

@import <FrameworkName>;

@ import < FrameworkName >;

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

#5


13  

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生成的接口标题名称”。该属性的值是您应该包含的名称。

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.

如果您的“产品模块名称”属性(在默认情况下依赖于此属性的属性)取决于您是否为test/debug/release/etc(在我的情况下)编译,那么您可以通过设置自定义名称使该属性独立于该变量。

#6


9  

Importing Swift file inside Objective-c can cause this error, if it doesn't import properly.

在Objective-c中导入Swift文件会导致这个错误,如果它不正确导入的话。

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 file inside Objective-C project. It would've created a bridging header automatically.

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

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

在目标->构建设置中检查Objective-C生成的接口头名称。

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

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

基于以上,我将导入KJExpandable-Swift。h。

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

你将TargetName-Swift。根据您的项目名称或您可能添加并运行的另一个目标,TargetName的不同之处。

As below my target is KJExpandable, so it's KJExpandable-Swift.h
如何将Swift代码导入Objective-C?

我的目标是KJExpandable,所以它是KJExpandable- swift .h。

#7


7  

First Step:-

第一步:-

Select Project Target -> Build Setting -> Search('Define') -> Define Module update value No to Yes

选择项目目标->构建设置->搜索('Define') ->定义模块更新值不为Yes。

"Defines Module": YES.

“定义模块”:是的。

"Always Embed Swift Standard Libraries" : YES.

“总是嵌入快速的标准库”:是的。

"Install Objective-C Compatibility Header" : YES.

“安装Objective-C兼容性头”:是的。

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

Second Step:-

第二步:-

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

在Objective C中添加Swift文件类。h”下面的文件

#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

进口的ProjectName(项目名称)迅速。在Objective C中。m”文件

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

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

#import "ProjectName-Swift.h"

#8


3  

Checkout the pre-release notes about Swift and Objective C in the same project

在同一个项目中,签出关于Swift和Objective C的预发布说明。

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

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

You should be importing

你应该导入

#import "SCLAlertView-Swift.h"

#9


2  

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生成的接口标题名称”(假设是MyApp-Swift.h),并在您试图访问Swift api的源文件中导入该设置的值(#import“MyApp-Swift.h”)。

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生成的接口标题名称”设置的值字段中双击,您可以看到它。

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”。

#10


1  

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

如果您在同一个框架内将Swift代码导入到您的Objective-C文件中,有一个警告。你必须指定框架名称和尖括号:

#import <MyFramework/MyFramework-Swift.h>

#进口< MyFramework / MyFramework-Swift.h >

MyFramework here 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 #import is 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.h in the Headers directory.

简单地添加# MyFramework-Swift进口”。h”是行不通的。如果您检查构建的产品目录(在添加#import之前,您至少有一个成功的构建,并且在目标中有一些Swift代码),那么您仍然可以看到文件myframeworkswift。h在标题目录中。

#11


0  

If you want to use Swift file into Objective-C class, so from Xcode 8 onwards you can follow below steps:

如果您希望将Swift文件应用到Objective-C类中,那么从Xcode 8开始,您可以按照以下步骤操作:

If you have created the project in Objective-C:

如果你在Objective-C中创建了这个项目:

  1. Create new Swift file
  2. 迅速创建新的文件
  3. Xcode will automatically prompt for Bridge-Header file
  4. Xcode将自动提示桥头文件。
  5. Generate it
  6. 产生它
  7. Import "ProjectName-Swift.h" in your Objective-C controller (import in implementation not in interface) (if your project has space in between name so use underscore "Project_Name-Swift.h")
  8. 导入“ProjectName-Swift。在Objective-C控制器中(导入在实现中而不是在接口中)(如果您的项目在名称之间有空格,那么请使用下划线“project_name - quickt.h”)
  9. You will be able to access your Objective-C class in Swift.
  10. 您将能够快速访问您的Objective-C类。

Compile it and if it will generate linker error like: compiled with newer version of Swift language (3.0) than previous files (2.0) for architecture x86_64 or armv 7

编译它,如果它会生成链接错误,比如:使用更新版本的Swift语言(3.0),而不是以前的文件(2.0),用于架构x86_64或armv7。

Make one more change in your

再做一次改变。

  1. Xcode -> Project -> Target -> Build Settings -> Use Legacy Swift Language Version -> Yes
  2. Xcode ->项目->目标->构建设置->使用Legacy Swift语言版本-> Yes。

Build and Run.

构建和运行。

#1


299  

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 @objc or inherit from NSObject.

这个单独的文件是一个自动生成的标头,它为您的项目中所有的Swift类定义了Objective-C接口,这些接口要么用@objc注释,要么从NSObject继承。

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

如果您的目标名称包含空格,请用下划线替换它们(例如:“我的项目”成为“My_Project-Swift.h”)

#2


103  

Instructions from the Apple website:

来自苹果官网的说明:

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

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

Under Build Settings, in Packaging, make sure the Defines Module setting 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:

在Build Settings下,在打包时,确保为该框架目标定义的模块设置为Yes。将该框架目标中的Swift代码导入到该框架目标中的任何Objective-C .m文件中,使用此语法并替换合适的名称:

#import "ProductName-Swift.h"

#进口“ProductName-Swift.h”

Revision:

修订:

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

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

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

在Objective-C .m文件中,您的目标中的Swift文件将会可见,其中包含了这个import语句。

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头文件中。相反,您可以转发声明一个Swift类以在Objective-C header中使用它。注意,在Objective-C中不能子类化一个Swift类。

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

#3


95  

Here's what to do :

以下是该做的:

  1. Create a new Project in Objective-C

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

  2. Create a new .swift file  

    创建一个新的.swift文件。

    • A popup window will appear and ask "Would You like to configure an Objective-C bridging Header".
    • 弹出窗口将出现并询问“您是否想配置一个Objective-C连接头”。
    • Choose Yes.
    • 选择Yes。
  3. Click on your Xcode Project file

    单击您的Xcode项目文件。

  4. Click on Build Settings

    点击建立设置

  5. Find the Search bar and search for Defines Module.

    找到搜索栏并搜索定义模块。

  6. Change value to Yes.

    变化值为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"

    在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"

#进口“YourProjectName-swift.h”

#4


23  

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

如果你正在使用Cocoapods并试图在ObjC项目中使用一个快速pod,你可以简单地做以下工作:

@import <FrameworkName>;

@ import < FrameworkName >;

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

#5


13  

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生成的接口标题名称”。该属性的值是您应该包含的名称。

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.

如果您的“产品模块名称”属性(在默认情况下依赖于此属性的属性)取决于您是否为test/debug/release/etc(在我的情况下)编译,那么您可以通过设置自定义名称使该属性独立于该变量。

#6


9  

Importing Swift file inside Objective-c can cause this error, if it doesn't import properly.

在Objective-c中导入Swift文件会导致这个错误,如果它不正确导入的话。

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 file inside Objective-C project. It would've created a bridging header automatically.

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

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

在目标->构建设置中检查Objective-C生成的接口头名称。

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

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

基于以上,我将导入KJExpandable-Swift。h。

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

你将TargetName-Swift。根据您的项目名称或您可能添加并运行的另一个目标,TargetName的不同之处。

As below my target is KJExpandable, so it's KJExpandable-Swift.h
如何将Swift代码导入Objective-C?

我的目标是KJExpandable,所以它是KJExpandable- swift .h。

#7


7  

First Step:-

第一步:-

Select Project Target -> Build Setting -> Search('Define') -> Define Module update value No to Yes

选择项目目标->构建设置->搜索('Define') ->定义模块更新值不为Yes。

"Defines Module": YES.

“定义模块”:是的。

"Always Embed Swift Standard Libraries" : YES.

“总是嵌入快速的标准库”:是的。

"Install Objective-C Compatibility Header" : YES.

“安装Objective-C兼容性头”:是的。

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

Second Step:-

第二步:-

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

在Objective C中添加Swift文件类。h”下面的文件

#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

进口的ProjectName(项目名称)迅速。在Objective C中。m”文件

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

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

#import "ProjectName-Swift.h"

#8


3  

Checkout the pre-release notes about Swift and Objective C in the same project

在同一个项目中,签出关于Swift和Objective C的预发布说明。

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

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

You should be importing

你应该导入

#import "SCLAlertView-Swift.h"

#9


2  

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生成的接口标题名称”(假设是MyApp-Swift.h),并在您试图访问Swift api的源文件中导入该设置的值(#import“MyApp-Swift.h”)。

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生成的接口标题名称”设置的值字段中双击,您可以看到它。

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”。

#10


1  

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

如果您在同一个框架内将Swift代码导入到您的Objective-C文件中,有一个警告。你必须指定框架名称和尖括号:

#import <MyFramework/MyFramework-Swift.h>

#进口< MyFramework / MyFramework-Swift.h >

MyFramework here 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 #import is 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.h in the Headers directory.

简单地添加# MyFramework-Swift进口”。h”是行不通的。如果您检查构建的产品目录(在添加#import之前,您至少有一个成功的构建,并且在目标中有一些Swift代码),那么您仍然可以看到文件myframeworkswift。h在标题目录中。

#11


0  

If you want to use Swift file into Objective-C class, so from Xcode 8 onwards you can follow below steps:

如果您希望将Swift文件应用到Objective-C类中,那么从Xcode 8开始,您可以按照以下步骤操作:

If you have created the project in Objective-C:

如果你在Objective-C中创建了这个项目:

  1. Create new Swift file
  2. 迅速创建新的文件
  3. Xcode will automatically prompt for Bridge-Header file
  4. Xcode将自动提示桥头文件。
  5. Generate it
  6. 产生它
  7. Import "ProjectName-Swift.h" in your Objective-C controller (import in implementation not in interface) (if your project has space in between name so use underscore "Project_Name-Swift.h")
  8. 导入“ProjectName-Swift。在Objective-C控制器中(导入在实现中而不是在接口中)(如果您的项目在名称之间有空格,那么请使用下划线“project_name - quickt.h”)
  9. You will be able to access your Objective-C class in Swift.
  10. 您将能够快速访问您的Objective-C类。

Compile it and if it will generate linker error like: compiled with newer version of Swift language (3.0) than previous files (2.0) for architecture x86_64 or armv 7

编译它,如果它会生成链接错误,比如:使用更新版本的Swift语言(3.0),而不是以前的文件(2.0),用于架构x86_64或armv7。

Make one more change in your

再做一次改变。

  1. Xcode -> Project -> Target -> Build Settings -> Use Legacy Swift Language Version -> Yes
  2. Xcode ->项目->目标->构建设置->使用Legacy Swift语言版本-> Yes。

Build and Run.

构建和运行。