ios 将 Project-Swift.h 导入 Objective-C 类...找不到文件

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

Importing Project-Swift.h into a Objective-C class...file not found

iosobjective-cxcodeswiftxcode6

提问by daspianist

I have a project that was started in Objective-C, and I am trying to import some Swift code into the same class files that I have previously written Objective-C in.

我有一个在 Objective-C 中启动的项目,我正在尝试将一些 Swift 代码导入到我之前编写 Objective-C 的同一个类文件中。

I have consulted the Apple docson using Swift and Objective-C in the same project, as well as SO questionlike this, but still no avail: I continue to get the file not found error after putting in #import "NewTestApp-Swift.h"(NewTestAppis the name of the Product and module).

我已经咨询了关于在同一个项目中使用 Swift 和 Objective-C的 Apple文档,以及这样的问题,但仍然无济于事:我在放入后继续得到文件未找到错误#import "NewTestApp-Swift.h"NewTestApp是产品的名称和模块)。

Here is what I have done so far:

这是我到目前为止所做的:

  1. In Define Modules, selected YESfor the app.
  2. Ensured that the Product Module name did not have any space in it (see screenshot below question)
  1. 在 中Define ModulesYES为应用程序选择。
  2. 确保产品模块名称中没有任何空格(请参阅下面的问题截图)

I have tried using #import "NewTestApp-Swift.h"inside ViewController.m, ViewController.hand AppDelegate.mbut none of them has worked.

我已经尝试使用#import "NewTestApp-Swift.h"ViewController.mViewController.hAppDelegate.m但他们没有工作过。

What else am I doing incorrectly? Thanks for your help.

我还有什么不正确的?谢谢你的帮助。



Screenshot of settings:

设置截图:

enter image description here

在此处输入图片说明



Errors that I am presently encountering:

我目前遇到的错误:

enter image description here

在此处输入图片说明

回答by bolnad

I was running into the same issue and couldn't get my project to import swift into obj-c classes. Using Xcode 6, (should work for Xcode 6+) and was able to do it in this way....

我遇到了同样的问题,无法让我的项目将 swift 导入 o​​bj-c 类。使用 Xcode 6,(应该适用于 Xcode 6+)并且能够以这种方式做到这一点......

  1. Any class that you need to access in the .h fileneeds to be a forward declaration like this
  1. 您需要在 .h 文件中访问的任何类都需要是这样的前向声明

@class MySwiftClass;

@class MySwiftClass;

  1. In the .m file ONLY, if the code is in the same project (module) then you need to import it with
  1. 在 .m 文件中,如果代码在同一个项目(模块)中,那么你需要用

#import "ProductModuleName-Swift.h"

#import "ProductModuleName-Swift.h"

Link to the apple documentation about it

链接到关于它的苹果文档

https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c

https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c

回答by ikocel1

If the Swift code is inside a Module (like in your case):

如果 Swift 代码在模块内(就像你的情况):

#import <ProductName/ProductModuleName-Swift.h>

If the Swift code is inside the project (mixed Swift and ObjC):

如果 Swift 代码在项目中(混合 Swift 和 ObjC):

#import <ProductModuleName-Swift.h>

In your case, you have to add this line in the *.m file:

在您的情况下,您必须在 *.m 文件中添加这一行:

#import <NewTestApp/NewTestApp-Swift.h>

IMPORTANT: look at the "<" in the import statement

重要提示:查看导入语句中的“<”

https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

回答by holm50

How I managed to import swift into objective-c:

我如何设法将 swift 导入到 Objective-c 中:

  • Defines Moduleset to YES (on project - not on target)
  • Product Module Nameset (on target - not on project)
  • In your ViewController.m import the swift code with:

    #import "MyProductModuleName-Swift.h"
    
  • Add a swift file to your objective-c project (File -> New -> Swift) and Xcode will create the bridging headerfrom objective-c to Swift but this is crucialfor making it work the other way around too - apparently.
  • 定义模块设置为 YES(在项目上 - 不在目标上)
  • 产品模块名称集(在目标上 - 不在项目上)
  • 在您的 ViewController.m 中导入 swift 代码:

    #import "MyProductModuleName-Swift.h"
    
  • 将 swift 文件添加到您的 Objective-c 项目(文件 -> 新建 -> Swift),Xcode 将创建从 Objective-c 到 Swift的桥接头但这对于使其反过来工作也至关重要- 显然。

For the last piece in this puzzle thanks to Swiftoverload for making me aware of actually adding a Swift file via Xcode GUI and not just dragging and dropping existing swift-files into my project for making it work: http://nikolakirev.com/blog/using-swift-in-objective-c-project

对于这个谜题的最后一部分,感谢 Swiftoverload 让我意识到实际上是通过 Xcode GUI 添加一个 Swift 文件,而不仅仅是将现有的 swift 文件拖放到我的项目中以使其工作:http: //nikolakirev.com/blog /using-swift-in-objective-c-project

回答by petershine

Using Xcode 8.2.1 and if you look at Project> Build Settings > Objective-C Generated Interface Header Name, there it shows only oneheader file named like Product-Swift.h

使用 Xcode 8.2.1,如果您查看Project> Build Settings > Objective-C Generated Interface Header Name,它只会显示一个名为Product-Swift.h 的头文件

This means that instead of importing each modules separately from Objective-C .m file, using individual -Swift.h file, you just import one Product-Swift.h which aggregated all Swift modules.

这意味着不是从 Objective-C .m 文件中单独导入每个模块,而是使用单独的 -Swift.h 文件,您只需导入一个Product-Swift.h,它聚合了所有 Swift 模块。

I encountered the same problem by looking for traditional way of importing modules separately, but the current version of Xcode and Swift 3 changed it to use only one header for all module importing.

我在寻找单独导入模块的传统方式时遇到了同样的问题,但当前版本的 Xcode 和 Swift 3 将其更改为仅使用一个标头进行所有模块导入。

回答by Josh B

I was having problems importing Swift into an Objective-C project. In the end I looked into the Derivied Data folder to check what Xcode 7 was generating. The filename was completely different to the one I was expecting.

我在将 Swift 导入 Objective-C 项目时遇到问题。最后,我查看了 Derivied Data 文件夹以检查 Xcode 7 生成的内容。文件名与我期望的完全不同。

Once I had the actual filename I imported that and the project could build.

一旦我有了实际的文件名,我就导入了它,项目就可以构建了。

回答by Jakehao

Spent an hour on this issue, following these steps will help you to understand what's missing:

在这个问题上花了一个小时,按照以下步骤操作将帮助您了解缺少的内容:

  1. Open Xcode preference and navigate to DerivedData folder
  2. Search for "swift.h" in finder
  3. If you can not find any project-swift.h file, this file haven't been generated. You usually need to add @objc to one of your swift class and successfully build the app, only then will Xcode generate this file
  4. If you found "xxx-swift.h" file, make sure your import statement contains the correct name.
  1. 打开 Xcode 首选项并导航到 DerivedData 文件夹
  2. 在查找器中搜索“swift.h”
  3. 如果找不到任何 project-swift.h 文件,则该文件尚未生成。您通常需要将 @objc 添加到您的 swift 类之一并成功构建应用程序,然后 Xcode 才会生成此文件
  4. 如果您找到“xxx-swift.h”文件,请确保您的导入语句包含正确的名称。

how-to-locate-swift-file

如何定位 swift 文件

回答by yoAlex5

-Swift.hshould be created by Xcode automatically if Swift code expose an API via @objcor @objcMembers[About]

-Swift.h如果 Swift 代码通过@objc[About]公开 API,则应由 Xcode 自动创建@objcMembers

Usually a location looks like

通常一个位置看起来像

~/Library/Developer/Xcode/DerivedData/
  ProductModuleName-foo/
    Build/
      Intermediates.noindex/
        ProductModuleName.build/
          Debug-iphoneos/
            ProductModuleName.build/
              DerivedSources/
                ProductModuleName-Swift.h

It can be changed by

它可以通过改变

Project editor -> select a target -> Build Settings -> Per-configuration Intermediate Build Files Path

By default the value is $(PROJECT_TEMP_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)

默认值为 $(PROJECT_TEMP_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)

回答by YSR fan

DEFINE MODULES: YES and import "ProjectName-Swift.h" in .m file of Obj-C class

定义模块:是并在 Obj-C 类的 .m 文件中导入“ProjectName-Swift.h”

This worked for me to access Swift classes in Obj-c.

这对我在 Obj-c 中访问 Swift 类很有用。

回答by Gurunath Sripad

During development, there might be a possibility that you would have any swift class that is not completely implemented due to which there might be some syntax errors. The swift header file will be available for use only if all the swift files are error free.

在开发过程中,您可能会遇到任何未完全实现的 swift 类,因此可能会出现一些语法错误。仅当所有 swift 文件都没有错误时,swift 头文件才可用。

Make sure there are no syntax errors in the swift files and then try to import the swift header in the objective - c file

确保 swift 文件中没有语法错误,然后尝试在目标 - c 文件中导入 swift 标头

回答by Claytog

Importing the header file, i.e.

导入头文件,即

#import "<ProjectName>-Swift.h" 

within the .h file generated an error, stating:

在 .h 文件中生成了一个错误,说明:

-Swift.h' file not found

-Swift.h' 文件未找到

and the build failed.

并且构建失败。

Instead use:

而是使用:

#import "<ProjectName>-Swift.h"

within the .m file, and even though the same error appears, running the project anyway suppresses the error.

在 .m 文件中,即使出现相同的错误,运行该项目仍会抑制该错误。

The swift classes are then available within the .m file.

然后在 .m 文件中可以使用 swift 类。