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

时间:2023-01-23 17:43:34

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


309  

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


105  

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下,在Packaging中,确保框架目标的定义模块设置为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.

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

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


99  

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委托中,添加以下内容:#导入“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


25  

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

如果你在一个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.

基于以上,我将导入kjexpand- 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。h,其中TargetName根据您的项目名称或您可能在其上添加和运行的另一个目标而有所不同。

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

因为在我的目标下面是KJExpandable,所以它是kjexpandswift .h

#7


7  

First Step:-

第一步:-

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

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

"Defines Module": YES.

“定义模块”:是的。

"Always Embed Swift Standard Libraries" : YES.

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

"Install Objective-C Compatibility Header" : YES.

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

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

Second Step:-

第二步:-

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

在目标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(项目名称)迅速。h在目标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.

在要构建的目标的构建设置中(假设是MyApp-Swift.h)搜索“Objective-C生成的接口头名”,并将该设置的值(#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生成的接口头名”设置的值字段,就可以看到它。

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“myapp - swift .h”。

#10


2  

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。h"在你的Objective-C控制器中(在实现中导入而不是在接口中)(如果你的项目在名称之间有空格,那么使用下划线“Project_Name-Swift.h”)
  9. You will be able to access your Objective-C class in Swift.
  10. 您将能够使用Swift访问您的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)编译,而不是以前的架构x86_64或armv7文件(2.0)

Make one more change in your

再做一次改变。

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

Build and Run.

构建和运行。

#1


309  

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


105  

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下,在Packaging中,确保框架目标的定义模块设置为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.

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

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


99  

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委托中,添加以下内容:#导入“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


25  

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

如果你在一个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.

基于以上,我将导入kjexpand- 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。h,其中TargetName根据您的项目名称或您可能在其上添加和运行的另一个目标而有所不同。

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

因为在我的目标下面是KJExpandable,所以它是kjexpandswift .h

#7


7  

First Step:-

第一步:-

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

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

"Defines Module": YES.

“定义模块”:是的。

"Always Embed Swift Standard Libraries" : YES.

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

"Install Objective-C Compatibility Header" : YES.

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

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

Second Step:-

第二步:-

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

在目标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(项目名称)迅速。h在目标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.

在要构建的目标的构建设置中(假设是MyApp-Swift.h)搜索“Objective-C生成的接口头名”,并将该设置的值(#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生成的接口头名”设置的值字段,就可以看到它。

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“myapp - swift .h”。

#10


2  

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。h"在你的Objective-C控制器中(在实现中导入而不是在接口中)(如果你的项目在名称之间有空格,那么使用下划线“Project_Name-Swift.h”)
  9. You will be able to access your Objective-C class in Swift.
  10. 您将能够使用Swift访问您的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)编译,而不是以前的架构x86_64或armv7文件(2.0)

Make one more change in your

再做一次改变。

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

Build and Run.

构建和运行。