ios 在 xcode8 中找不到 ModuleName-Swift.h 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40484816/
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
ModuleName-Swift.h file not found in xcode8
提问by LucioB
I'm trying to work on a mixed Swift and ObjectiveC project with no luck.
我正在尝试在一个没有运气的混合 Swift 和 ObjectiveC 项目上工作。
My project is made of 6 targets:
我的项目由 6 个目标组成:
- App
- Core
- CoreTest
- Summary
- WatchKit Extension
- WatchKit App
- 应用程序
- 核
- 核心测试
- 概括
- WatchKit 扩展
- WatchKit 应用程序
I've been able to add a Swift class that has one target membership (App) and uses ObjectiveC code passing from the App-Bridging-Header.hThe "ObjectiveC into Swift code" paradigm works like a charm.
我已经能够添加一个 Swift 类,它具有一个目标成员 (App) 并使用从App-Bridging-Header.h传递的 ObjectiveC 代码 。“ObjectiveC 到 Swift 代码”范式的作用就像一个魅力。
Instead, I'm struggling with the "Swift into ObjectiveC code" paradigm. In the specific case I need to use a new Swift class inside an ObjectiveC .m file that is member of four targets.
相反,我正在为“Swift 到 ObjectiveC 代码”的范式而苦苦挣扎。在特定情况下,我需要在作为四个目标成员的 ObjectiveC .m 文件中使用新的 Swift 类。
What I've been doing so far is this:
到目前为止我一直在做的是:
- Add a new Swift file to the Core project specifying the four memberships (App, Core, Summary, WatchKit Ext)
- XCode asks to create -Bridging-Header.hfor three projects Core, Summary and WatchKit Ext (App-Bridging-Header.h already exists since it was used previously), and I consent (don't know why it creates those files inside the same group folder where I add my Swift class but nevermind)
- I create the Swift class adding the @objckey before class
- I go check if Objective-C Generated Interface Header Nameis set for all modules, and yes it is
- I go check if Defines Moduleis set to Noin all modules and set to Yesin the main project, and yes it is
- I write down the imports for the -Swift.hfiles for all the four targets
- Inside my Objective-C class I try to use my new Swift class, apparently it seems to work (even autocompletion)
- I try to build the project but it fails saying that the -Swift.hfile is not found
- 向 Core 项目添加一个新的 Swift 文件,指定四个成员身份(App、Core、Summary、WatchKit Ext)
- XCode 要求为Core、Summary 和 WatchKit Ext 三个项目创建-Bridging-Header.h(App-Bridging-Header.h 已经存在,因为它以前被使用过),我同意(不知道为什么它会在里面创建这些文件)我在其中添加 Swift 类的同一个组文件夹,但没关系)
- 我创建了 Swift 类,在类之前添加了@objc键
- 我去检查是否为所有模块设置了Objective-C Generated Interface Header Name,是的
- 我去检查是否在所有模块中定义模块设置为否并在主项目中设置为是,是的
- 我写下了所有四个目标的-Swift.h文件的导入
- 在我的 Objective-C 类中,我尝试使用我的新 Swift 类,显然它似乎有效(甚至自动完成)
- 我尝试构建该项目,但没有找到-Swift.h文件
I've followed the Mix and Match guide of Appleand many SO threads including this onethat seems apparently to address the same problem of mine but it doesn't work.
我遵循了Apple的Mix and Match 指南和许多 SO 线程,包括这个似乎显然解决了我的相同问题的线程,但它不起作用。
Also have seen thisand thisbut didn't work.
Have tried also to Delete Derived Data as well as cleaning the whole project several times with no luck.
还尝试过多次删除派生数据以及清理整个项目,但都没有成功。
EDIT 1 : More details
编辑 1:更多细节
This is the declaration of my Swift class import RealmSwift
这是我的 Swift 类 import RealmSwift 的声明
@objc class MyClass: Object {}
This is the snippet of usage of the Swift class inside ObjectiveC
这是ObjectiveC中Swift类的使用片段
MyClass *app = [[MyClass alloc] init];
If I avoid using #import ...-Swift.h the build fails saying that I'm making use of an Undeclared idenfier "MyClass" and this is sound.
如果我避免使用 #import ...-Swift.h 构建失败,说我正在使用一个未声明的标识符“MyClass”,这是合理的。
If I do use #import ...-Swift.h files it says that module X cannot find module Y and so on.
如果我确实使用 #import ...-Swift.h 文件,它会说模块 X 找不到模块 Y 等等。
EDIT 2 : Swift module name
编辑 2:Swift 模块名称
By looking at the SWIFT_OBJC_INTERFACE_HEADER_NAMEproperty of each target I've seen that it is built using this syntax $(SWIFT_MODULE_NAME)-Swift.hdo I need to change SWIFT_MODULE_NAMEto MODULE_NAME?
通过查看每个目标的SWIFT_OBJC_INTERFACE_HEADER_NAME属性,我看到它是使用此语法$(SWIFT_MODULE_NAME)-Swift.h 构建的,我是否需要将SWIFT_MODULE_NAME更改为MODULE_NAME?
EDIT 3 : Imports
编辑 3:进口
This is the set of imports for Swift headers in .m ObjectiveC file. I'm using the module names specified in relative Build Settings properties. Notice that one has '_' character since WatchKit Extension target has a space in the target name.
这是 .m ObjectiveC 文件中 Swift 头文件的导入集。我正在使用相关构建设置属性中指定的模块名称。请注意,因为 WatchKit 扩展目标在目标名称中有一个空格,所以一个有 '_' 字符。
#import "ProjectName_App-Swift.h"
#import "ProjectName_Core-Swift.h"
#import "ProjectName_Summary-Swift.h"
#import "ProjectName_WatchKit_Extension-Swift.h"
EDIT 4 : Cross visibility of -Swift.h files
编辑 4:-Swift.h 文件的交叉可见性
I've tryed the following:
我尝试了以下方法:
- Added a Swift class only in target App
- XCode created the Bridging Header
- Inserted #import "ProjectName_App-Swift.h" in ObjectiveC file used only by App
- Made use of Swift class inside this file
- It compiles! And I'm able to use the Swift class
- 仅在目标应用程序中添加了 Swift 类
- XCode 创建了桥接头
- 在仅由应用程序使用的 ObjectiveC 文件中插入 #import "ProjectName_App-Swift.h"
- 在此文件中使用 Swift 类
- 它编译!我可以使用 Swift 类
When the Swift class is defined for multiple targets and ObjectiveC file is used by those multiple targets it doesn't work. The build errors are like this: Target X -> "ProjectName_TargetY-Swift.h" file not found. Seems like a target cannot see other targets -Swift.h files.
当为多个目标定义 Swift 类并且这些多个目标使用 ObjectiveC 文件时,它不起作用。构建错误是这样的:找不到目标 X -> "ProjectName_TargetY-Swift.h" 文件。似乎一个目标看不到其他目标 -Swift.h 文件。
What am I doing wrong? Thanks for your help
我究竟做错了什么?谢谢你的帮助
回答by LucioB
The assumption I made in Edit 4 turned out to be the correct one. If a project has multiple targets the "-Swift.h" files cannot be imported all in one .m ObjectiveC file.
我在 Edit 4 中所做的假设结果证明是正确的。如果一个项目有多个目标,则无法将“-Swift.h”文件全部导入到一个 .m ObjectiveC 文件中。
So there is one solution that must be adopted and it is to change the SWIFT_OBJC_INTERFACE_HEADER_NAMEbuild setting and making it the same across different targets. To do so change the instruction that generates this property from $(SWIFT_MODULE_NAME)-Swift.hto $(PROJECT_NAME)-Swift.has explained here
因此,必须采用一种解决方案,即更改SWIFT_OBJC_INTERFACE_HEADER_NAME构建设置并使其在不同目标上相同。这样做的改变,产生从这个属性的指令$(SWIFT_MODULE_NAME)-Swift.h到$(PROJECT_NAME)-Swift.h作为解释这里
It is also possible to set the Product Module Name setting in Build Settings to be the same across your modules (I set it to $(PROJECT_NAME)), so that the -Swift.h file that is generated has the same name across all modules. This eliminates the need for adding/checking preprocessor macros.
还可以将 Build Settings 中的 Product Module Name 设置在您的模块中设置为相同(我将其设置为 $(PROJECT_NAME)),以便生成的 -Swift.h 文件在所有模块中具有相同的名称. 这消除了添加/检查预处理器宏的需要。
After doing this Clean Build Folder by pressing Alt and going into Product menu. Since name of header is shared among targets now it can be imported once in the .m ObjectiveC file and all targets can benefit from Swift classes.
通过按 Alt 并进入产品菜单完成此清理构建文件夹后。由于标头的名称现在在目标之间共享,因此可以在 .m ObjectiveC 文件中导入一次,并且所有目标都可以从 Swift 类中受益。
If after building it still shows the error, ensure that the header can be reached from XCode by Cmd clicking on its name. It should open a file that contains code similar to this:
如果构建后仍然显示错误,请确保可以通过 Cmd 单击其名称从 XCode 访问标题。它应该打开一个包含类似于以下代码的文件:
SWIFT_CLASS("_TtC27ProjectName_Summary11MyClass")
@interface MyClass : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end
If need to ensure that those headers are being generated open a terminal and use this command
如果需要确保生成这些标头,请打开终端并使用此命令
find ~/Library/Developer/Xcode/DerivedData -name "*Swift.h"
You should see one header for each target
您应该为每个目标看到一个标题
Another issue that happened to me after those changes is that it started giving errors on ObjectiveC code that I didn't touch. The problem was due to the position of the import, as reported here:
在这些更改之后发生在我身上的另一个问题是它开始在我没有触及的 ObjectiveC 代码上给出错误。这个问题是由于进口的位置,据报道在这里:
Exactly where at the top of a .m file you #import the hidden bridging header can make a difference. The usual sign of trouble is that you get an “Unknown type name” compile error, where the unknown type is a class declared in Objective-C. The solution is to #import the .h file containing the declaration for the unknown type in your Objective-C files as well, before you #import the hidden bridging header. Having to do this can be an annoyance, especially if the Objective-C file in question has no need to know about this class, but it resolves the issue and allows compilation to proceed.
在 .m 文件顶部的确切位置 #import 隐藏的桥接头可以产生影响。通常的麻烦迹象是你得到一个“未知类型名称”编译错误,其中未知类型是在 Objective-C 中声明的类。解决方案是在#import 隐藏桥接头之前#import 包含Objective-C 文件中未知类型声明的.h 文件。必须这样做可能会很烦人,特别是如果有问题的 Objective-C 文件不需要了解此类,但它解决了问题并允许继续编译。
At the very end the code compiles and runs on device and simulator!
最后,代码编译并在设备和模拟器上运行!
回答by Jens
This is not a solution for the question but since many might end up on this page looking for the problem that the <ModuleName>-Swift.h
file is not found (just like me) I'd like to add one more possible reason:
这不是问题的解决方案,但因为很多人可能会落得如此页面查找问题是,在<ModuleName>-Swift.h
没有找到文件(就像我),我想补充一个可能的原因:
When there is a compiler issue in a swift file the header might not get generated by Xcode and thus the file is missing.
当 swift 文件中存在编译器问题时,Xcode 可能不会生成标头,因此文件丢失。
At least in my case the <ModuleName>-Swift.h not found
was gone once I fixed the compile error in the Swift file.
至少在我的情况下,<ModuleName>-Swift.h not found
一旦我修复了 Swift 文件中的编译错误,它就消失了。
回答by cornr
The solution for me was to import the generated swift header via <>
syntax:
我的解决方案是通过<>
语法导入生成的 swift 标头:
#import <OtherTargetWithSwiftClass/OtherTargetWithSwiftClass-Swift.h>
回答by retrocoder84
I had similar issue and almost ended up spending a whole day figuring out the issue while following suggestions from fellow developers on StackOverflow and few other sources.
我遇到了类似的问题,几乎花了一整天的时间来解决这个问题,同时遵循 StackOverflow 和其他一些来源上的开发人员的建议。
I was upgrading from Xcode 7.3.1 to Xcode 8.2.1 and Swift 2.2 to Swift 3.0.1.
In my case, I figured out the PROJECT_NAME-Swift.h
file didn't get created at:
我从 Xcode 7.3.1 升级到 Xcode 8.2.1,从 Swift 2.2 升级到 Swift 3.0.1。
就我而言,我发现PROJECT_NAME-Swift.h
文件不是在以下位置创建的:
~/Library/Developer/Xcode/DerivedData/PRODUCT-NAME-hdkrxixrxbiorwesffpqvefxsrzl/Build/Intermediates/PRODUCT-NAME.build/Debug/PRODUCT-NAME.build/Objects-normal/x86_64/PRODUCT-NAME-Swift.h.
~/Library/Developer/Xcode/DerivedData/PRODUCT-NAME-hdkrxixrxbiorwesffpqvefxsrzl/Build/Intermediates/PRODUCT-NAME.build/Debug/PRODUCT-NAME.build/Objects-normal/x86_64/PRODUCT-NAME-Swift.h.
I guess it somehow tries to refer to this header file from the above path and hence the error. I manually copied it. Didn't do clean build after this. This resolved my issue and the swift header file started getting created for further compilation and builds.
我想它以某种方式尝试从上述路径引用此头文件,因此出现错误。我手动复制的。在此之后没有进行干净的构建。这解决了我的问题,并且开始创建 swift 头文件以进行进一步的编译和构建。
回答by Ted
For my case, it runs fine in Xcode 8 but files with
就我而言,它在 Xcode 8 中运行良好,但文件带有
#import "Project-Swift.h"
has missing imports as described by LucioB
缺少 LucioB 描述的导入
Using Xcode 9removes those errors
使用Xcode 9可以消除这些错误
回答by ScottyB
In my case, I have a workspace with an iOS app subproject, a framework subproject, and a CocoaPods Pods subproject. The "ModuleName-Swift.h file not found" error occurred in my framework subproject because the file hadn't yet been created by the Pods subproject.
就我而言,我有一个包含 iOS 应用程序子项目、框架子项目和 CocoaPods Pods 子项目的工作区。我的框架子项目中出现“ModuleName-Swift.h 文件未找到”错误,因为该文件尚未由 Pods 子项目创建。
I'm not sure if there's a way to tell Xcode about dependencies between subprojects, but I simply re-ordered them in the left-side Project Navigator:
我不确定是否有办法告诉 Xcode 子项目之间的依赖关系,但我只是在左侧的项目导航器中重新排序它们:
- Pods
- framework
- iOS app
- 豆荚
- 框架
- iOS应用
Worked great after that!
之后效果很好!
回答by Shourob Datta
In my case, I declared import statement into .h file
就我而言,我在 .h 文件中声明了 import 语句
Then I change
那我改
#import "ProjectName_App-Swift.h"
into implementation file(.m)
进入实现文件(.m)
Clean and restart Xcode Working
清理并重新启动 Xcode 工作