XCode 在某些文件中找不到 Swift 桥接头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31513544/
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
XCode does not find Swift bridging header file in some files
提问by John S.
I do have a problem in my iOs application with XCode. It has been coded in Objective-C, and now I am working on migrating files in Swift, one after another. The code is shared between team members with a versioning system (git).
我在使用 XCode 的 iOs 应用程序中确实遇到了问题。它是用 Objective-C 编写的,现在我正在 Swift 中一个接一个地迁移文件。代码在团队成员之间使用版本控制系统 (git) 共享。
I started with the core classes and I proceed like this :
我从核心类开始,我是这样进行的:
- I write the new class in a Swift file, ex. MyClass.swift (with
@objc
prefix keyword) - I replace
#import "MyClass.h"
by@class MyClass;
in header files - I add
#import "MyProject-Swift.h"
in implementation files (.m) needing it - I delete MyClass.h and MyClass.m files
- I clean and build the project
- 我在 Swift 文件中编写了新类,例如。MyClass.swift(带
@objc
前缀关键字) - 我在头文件中替换
#import "MyClass.h"
为@class MyClass;
- 我添加
#import "MyProject-Swift.h"
了需要它的实现文件 (.m) - 我删除 MyClass.h 和 MyClass.m 文件
- 我清理并构建项目
This process has worked several times, but for some reasons some #import "MyProject-Swift.h"
do not work, and I get the error : 'MyProject-Swift.h' file not found.
这个过程已经运行了几次,但由于某些原因,有些#import "MyProject-Swift.h"
不起作用,我收到错误:'MyProject-Swift.h' file not found。
According to the different files causing the problem, sometimes, Ctrl+Click will open the "MyProject-Swift.h" file, sometimes not (no matter of the "not found error").
根据导致问题的文件不同,有时,Ctrl+单击会打开“MyProject-Swift.h”文件,有时不会(无论“未找到错误”)。
But the file exists and the translated Swift code is present inside it.
但是该文件存在并且其中包含翻译后的 Swift 代码。
Another thing really weird, I tried to re-create new .h and .m files from those using #import "MyProject-Swift.h"
stuff and having the problem, and after that it works (sometimes not, and I get other errors) !
另一件非常奇怪的事情,我试图从那些使用#import "MyProject-Swift.h"
东西并遇到问题的人重新创建新的 .h 和 .m 文件,然后它就可以工作了(有时不行,而且我遇到了其他错误)!
As it works in some cases, I really don't get why it is causing problems for other cases. Of course I searched among dozens of topics but did not find anyone with the same problem.
由于它在某些情况下有效,我真的不明白为什么它会导致其他情况出现问题。当然,我搜索了几十个主题,但没有找到任何有同样问题的人。
Could it be an XCode settings issue, or because of different XCode versions between team developers ?
可能是 XCode 设置问题,还是因为团队开发人员之间的 XCode 版本不同?
Any idea ?
任何的想法 ?
Thanks
谢谢
Edited :
编辑:
I complete the issue description with another point of view.
我从另一个角度完成了问题描述。
I do have the project in a working state : uses some of my new Swift files, it builds and runs well (some Objective-C files use Swift files).
我的项目确实处于工作状态:使用我的一些新 Swift 文件,它构建和运行良好(一些 Objective-C 文件使用 Swift 文件)。
I know a certain .m file having the issue :
我知道某个 .m 文件有问题:
- I add
#import "MyProject-Swift.h"
at the beginning, nothing else - XCode says 'MyProject-Swift.h' file not found, Ctrl+Click does not work, and the project won't build
- But, if I click on the button with four little squares (top left of the editor), in the displayed menu I have "Includes", the "MyProject-Swift.h" is listed under it, and clicking on "MyProject-Swift.h" opens the file !
- 我
#import "MyProject-Swift.h"
在开头加上,没有别的 - XCode 显示未找到“MyProject-Swift.h”文件,Ctrl+单击不起作用,项目将无法构建
- 但是,如果我点击带有四个小方块的按钮(编辑器的左上角),在显示的菜单中我有“包含”,“MyProject-Swift.h”列在它下面,然后点击“MyProject-Swift .h”打开文件!
Replacing #import "MyProject-Swift.h"
by #import <MyProject-Swift.h>
won't change anything.
更换#import "MyProject-Swift.h"
由#import <MyProject-Swift.h>
不会改变任何东西。
I also have this case sometimes : Ctrl+Click opens "MyProject-Swift.h", but XCode says 'MyProject-Swift.h' file not found(it won't build as well).
我有时也有这种情况:Ctrl+单击打开“MyProject-Swift.h”,但 XCode 说找不到“MyProject-Swift.h”文件(它也不会构建)。
采纳答案by John S.
I finally came up with the solution on my own : the project.pbxproj file was kind of corrupted after some git merge I guess. Some files of the project were referenced twice in that file, so I deleted the ones I thought being bad (maybe randomly chosen is closer from the truth).
我终于想出了自己的解决方案:我猜在一些 git merge 之后,project.pbxproj 文件有点损坏。该项目的某些文件在该文件中被引用了两次,所以我删除了我认为不好的那些(可能随机选择的更接近事实)。
Now it's working like a charm.
现在它就像一个魅力。
In fact this had no effect on the project until I tried to migrate files to Swift !
事实上,在我尝试将文件迁移到 Swift 之前,这对项目没有影响!
It would be a great idea to have a tool or function in XCode to reset this project.pbxproj file to the current things we have.
在 XCode 中有一个工具或函数来将此 project.pbxproj 文件重置为我们拥有的当前内容将是一个好主意。
回答by Nikolay Shubenkov
basing on thatasumption https://stackoverflow.com/a/31540611/2150954I solved this problem in this simple way
立足于那个asumption https://stackoverflow.com/a/31540611/2150954我在这解决了这个问题的简单方法
Find objective C file, in which compiler claims that it can not find YourProject-Swift file there. Removeit file from project and then add.
找到目标 C 文件,编译器声称在该文件中找不到 YourProject-Swift 文件。从项目中删除它的文件,然后添加.
After that my project successfully compiled and run
之后我的项目成功编译并运行
回答by Bhumesh Purohit
Add a header file to your project, named [MyProjectName]-Bridging-Header.h.This will be the single header file where you import any Objective-C code you want your Swift code to have access to.
In your project build settings, find Swift Compiler – Code Generation, and next to Objective-C Bridging Header add the path to your bridging header file, from the project's root folder. So it could by MyProject/MyProject-Bridging-Header.hor simply MyProject-Bridging-Header.hif the file lives in the project root folder.
向您的项目添加一个名为[MyProjectName]-Bridging-Header.h的头文件。这将是单个头文件,您可以在其中导入您希望 Swift 代码访问的任何 Objective-C 代码。
在您的项目构建设置中,找到 Swift Compiler – Code Generation,然后在 Objective-C Bridging Header 旁边从项目的根文件夹中添加到您的桥接头文件的路径。所以它可以通过MyProject/MyProject-Bridging-Header.h或简单的MyProject-Bridging-Header.h如果文件位于项目根文件夹中。
You only need one Bridging Header. Add your #import statements to this file, and your classes will now be available in your Swift code without any extra import statements.
您只需要一个桥接头。将您的 #import 语句添加到此文件中,您的类现在可以在您的 Swift 代码中使用,而无需任何额外的导入语句。
回答by prad
If you have two targetsand you keep getting compile/semantic errors for newly edited swift files used in ObjC code, this could be the problem -
如果您有两个目标,并且在 ObjC 代码中使用的新编辑的 swift 文件不断出现编译/语义错误,这可能是问题所在 -
You got two <ProjectName>-swift.h
files. Like in mycase:
MyProject-swift.h
and MyProjectSandbox-swift.h
These are the files with all the swift classes and methods.
(These open up when you Command-click on any swift class from Obj-C code).
你有两个<ProjectName>-swift.h
文件。就像在 mycase:
MyProject-swift.h
andMyProjectSandbox-swift.h
这些是包含所有 swift 类和方法的文件。
(当您在 Obj-C 代码中按 Command 键单击任何 swift 类时,这些会打开)。
So I am working on the Sandbox
target.
I found that, when I made changes to swift files and clean-build, only MyProjectSandbox-swift.h
was being updated.
But the main problem was the ObjC code was referring to MyProject-swift.h
file which does not update as I was only rebuilding the (sandbox)target I was working on..
所以我正在研究Sandbox
目标。我发现,当我对 swift 文件和 clean-build 进行更改时,只有MyProjectSandbox-swift.h
被更新。但主要问题是 ObjC 代码指的MyProject-swift.h
是没有更新的文件,因为我只是在重建我正在处理的(沙箱)目标。
TL;DR;
TL; 博士;
So the solution is to make sure you clean and rebuild BOTHyour targets so that both (MyProject-swift.h
and MyProjectSandbox-swift.h
) are updated with latest swift code and classes.
因此,解决方案是确保您清理和重建两个目标,以便 (MyProject-swift.h
和MyProjectSandbox-swift.h
) 都使用最新的 swift 代码和类进行更新。
This will update both files and your ObjC code won't show any irrelevant compile errors for swift files.
这将更新这两个文件,并且您的 ObjC 代码不会显示任何与 swift 文件无关的编译错误。