ios Swift 桥接头导入问题

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

Swift Bridging Header import issue

iosswiftxcode6bridging-headerobjective-c-swift-bridge

提问by Nikita Pronchik

Following instructions, I've created a bridging header and added to my project. Unfortunately, the following error occurred:

按照说明,我创建了一个桥接头并添加到我的项目中。不幸的是,发生了以下错误:

:0: error: could not import Objective-C header '---path--to---header/....h'

:0: 错误: 无法导入 Objective-C 标头 '---path---to---header/....h'

Screenshot 1

截图 1

In build settings I added header search path string to the location of bridging header, but nothing helped.

在构建设置中,我将标头搜索路径字符串添加到桥接头的位置,但没有任何帮助。

Has anyone experienced this problem?

有没有人遇到过这个问题?

回答by Renan Kosicki

Be careful to add the file to the folder that your error is complaining! I've made the same mistake, if you create the file from Xcode, it will go to the folder: Project->Project->Header.h

小心地将文件添加到您的错误所抱怨的文件夹中!我犯了同样的错误,如果你从 Xcode 创建文件,它会转到文件夹:Project->Project->Header.h

And Xcode is looking for Project->Header.h

Xcode 正在寻找 Project->Header.h

That means you need to put the file inside your project folder (ProjectName->ProjectNameFolder)!

这意味着您需要将文件放在您的项目文件夹中(ProjectName->ProjectNameFolder)!

Hope that helps ;)

希望有帮助;)

UPDATED: I'm not sure if I got what you mean, but try this to solve your problem:
1. Delete all your bridging files that you created until now.
2. Select the main folder of project and hit new file->iOS->Header file.
3. Write your imports in the header file created.
4. Select the project inside Xcode->Build Settings, type in search field: bridging and put in the key SWIFT_OBJC_BRIDGING_HEADER the name of your header file or the path to it!

更新:我不确定我是否明白你的意思,但试试这个来解决你的问题:
1. 删除你迄今为止创建的所有桥接文件。
2.选择项目的主文件夹,然后点击新文件->iOS->头文件。
3. 在创建的头文件中写入您的导入。
4. 在 Xcode->Build Settings 中选择项目,在搜索字段中输入:bridging 并在键 SWIFT_OBJC_BRIDGING_HEADER 中输入您的头文件的名称或路径!

If you follow this steps, your header file will be created at the correct location!

如果您按照此步骤操作,您的头文件将在正确的位置创建!

:D Hope that helps!

:D 希望有帮助!

回答by Daniel Galasko

In my case this was actually an error as a result of a circular reference. I had a class imported in the bridging header, and that class' header file was importing the swift header (<MODULE_NAME>-Swift.h). I was doing this because in the Obj-C header file I needed to use a class that was declared in Swift, the solution was to simply use the @classdeclarative.

就我而言,这实际上是由于循环引用而导致的错误。我在桥接头中导入了一个类,该类的头文件正在导入 swift 头文件 ( <MODULE_NAME>-Swift.h)。我这样做是因为在 Obj-C 头文件中我需要使用一个在 Swift 中声明的类,解决方案是简单地使用@class声明。

So basically the error said "Failed to import bridging header", the error above it said <MODULE_NAME>-Swift.hfile not found, above that was an error pointing at a specific Obj-C Header file (namely a View Controller).

所以基本上错误说“无法导入桥接头”,上面的错误说<MODULE_NAME>-Swift.h找不到文件,上面的错误指向特定的Obj-C头文件(即视图控制器)。

Inspecting this file I noticed that it had the -Swift.h declared inside the header. Moving this import to the implementation resolved the issue. So I needed to use an object, lets call it MyObjectdefined in Swift, so I simply changed the header to say

检查这个文件,我注意到它在头文件中声明了 -Swift.h。将此导入移至实现解决了该问题。所以我需要使用一个对象,让我们MyObject在 Swift 中定义它,所以我只是改变了标题说

@class MyObject;

回答by variadics

Find the path at:

在以下位置找到路径:

Build Settings/Swift Compiler-Code Generation/Objective-C Bridging Header

构建设置/Swift 编译器-代码生成/Objective-C Bridging Header

and delete that file. Then you should be ok.

并删除该文件。那你应该没问题。

回答by Kyle Clegg

This will probably only affect a small percentage of people, but in my case my project was using CocoaPods and one of those pods had a sub spec with its own CocoaPods. The solution was to use full angle imports to reference any files in the sub-pods.

这可能只会影响一小部分人,但就我而言,我的项目使用的是 CocoaPods,其中一个 Pod 有一个带有自己的 CocoaPods 的子规范。解决方案是使用全角导入来引用子 Pod 中的任何文件。

#import <HexColors/HexColor.h>

Rather than

而不是

#import "HexColor.h"

回答by M.Othman

For me deleting the derived datafixed it , I noticed even if I check out from an old commit, the same issue happens.

对于我删除派生数据修复它,我注意到即使我从旧提交中签出,也会发生同样的问题。

You can reach that option form Window-> Projects .

您可以通过 Window-> Projects 访问该选项。

回答by G S

"we need to tell Xcodewhere to look for the header fileswe're listing in our bridging header. Find the Search Paths section, and change the project-level setting for User Header Search Paths, adding a recursive entry for the ‘Pods' directory: Pods/** " http://swiftalicio.us/2014/11/using-cocoapods-from-swift/

“我们需要告诉Xcode在哪里查找我们在桥接头中列出的头文件。找到 Search Paths 部分,并更改User Header Search Paths的项目级设置,为 'Pods' 添加一个递归条目目录:Pods/**” http://swiftalicio.us/2014/11/using-cocoapods-from-swift/

回答by Roland Keesom

For me it was because I forgot to add it to the Target's Build Settings.

对我来说这是因为我忘记将它添加到目标的构建设置中。

enter image description here

enter image description here

回答by douglas bumby

I've also experienced this problem and sadly it is just a bug in the SDK + Xcode. I talked to an engineer at WWDC, about this and a few other problems I was having with CloudKit. These bugs will be addressed in the next seed of Xcode.

我也遇到过这个问题,遗憾的是它只是 SDK + Xcode 中的一个错误。我与 WWDC 的一位工程师讨论了这个问题以及我在 CloudKit 中遇到的其他一些问题。这些错误将在 Xcode 的下一个种子中解决。

It's the fun part about using beta software.

这是使用测试版软件的有趣部分。

回答by user1105951

for others who have troubles to add swift class into objective-c project. this is what work for me :

对于那些在将 swift 类添加到 Objective-C 项目中遇到麻烦的人。这对我有用:

  1. create NEW swift file. this will make xcode to prompt if you want xcode to create all settings for mix swift-objective-c project including brigde-header.h for you. press yes.
  2. now, add your existing swift files you want to use in your project.
  3. in the implementation file you are going to use the swift class add : #import "YOURPROJECTNAME-swift.h" . this file xcode create for you. if your xcode project is myProject then "myProject-swift.h"
  1. 创建新的 swift 文件。这将使 xcode 提示您是否希望 xcode 为 mix swift-objective-c 项目创建所有设置,包括 brigde-header.h。按是。
  2. 现在,添加要在项目中使用的现有 swift 文件。
  3. 在实现文件中,您将使用 swift 类添加 : #import "YOURPROJECTNAME-swift.h" 。这个文件 xcode 为你创建。如果您的 xcode 项目是 myProject 那么“myProject-swift.h”

and that's it. now create the swift class in your code like it was objective-c.

就是这样。现在在你的代码中创建 swift 类,就像它是 Objective-c 一样。

回答by Nikolay Shubenkov

I imported in some files from bridgin header files from cocoapods not in a proper way.

我从 cocoapods 的 bridgin 头文件中以不正确的方式导入了一些文件。

Instead of importing

而不是进口

#import <SomeCocoaPod/SomeCocoaPod.h>

I wrote

我写

#import "SomeCocoaPod.h"

And this was my HUGE mistake

这是我的巨大错误