未找到混合 Swift 和 Objective-c 项目的 Xcode 8 生成的“ModuleName-Swift.h”标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39668619/
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 8 with mixed Swift and Objective-c project generated "ModuleName-Swift.h" header not found
提问by myuiviews
I have a project with mixed Swift and Objective-C in Xcode 8 that uses the generated "ModuleName-Swift.h" header file to import swift into Objective-c classes, but the preprocessor is not able to find the generated header file and throws an error on the import.
我在 Xcode 8 中有一个混合 Swift 和 Objective-C 的项目,它使用生成的“ModuleName-Swift.h”头文件将 swift 导入到 Objective-c 类中,但预处理器无法找到生成的头文件并抛出导入错误。
"Lexical or Preprocessor issue : 'ModuleName-Swift.h file not found'"
“词法或预处理器问题:'ModuleName-Swift.h 文件未找到'”
The project compiles just fine, but the preprocessor throws errors for the header not being found and for any Swift classes called inside the class there is no syntax highlighting or code completion. It's a struggle working with Swift classes in Objective-c that are unrecognized by Xcode 8, but yet compile just fine.
该项目编译得很好,但预处理器会因找不到头文件而引发错误,并且对于在类中调用的任何 Swift 类,没有语法突出显示或代码完成。在 Objective-c 中使用 Swift 类很难被 Xcode 8 识别,但编译得很好。
Any ideas on how to appease the preprocessor in Xcode 8?
关于如何安抚 Xcode 8 中的预处理器的任何想法?
采纳答案by Ernesto Fernandez
I had exactly the same issue. Found the solution after adding a new file aiming to only one target (by mistake) and noticing that it had no problem reading the Swift classes. So, if you have multiple targets, and since the migration you didn't have the need to build and run them, suggest you to do that.
我有完全相同的问题。在添加一个仅针对一个目标的新文件(错误地)并注意到读取 Swift 类没有问题后找到了解决方案。因此,如果您有多个目标,并且由于迁移您不需要构建和运行它们,建议您这样做。
回答by Mesut GUNES
Have this problem when we have multiple targets. If the Objective-c Generated Interface Header Namevariable in Swift compiler of newly created targets is different than the original target's value. Change it to the same value with original target. See the following:
当我们有多个目标时会出现这个问题。如果新创建目标的 Swift 编译器中的Objective-c Generated Interface Header Name变量与原始目标的值不同。将其更改为与原始目标相同的值。请参阅以下内容:
change, newtargetname-Swift.hto originaltargetname-Swift.hfor new target
将新目标的newtargetname-Swift.h更改为originaltargetname-Swift.h
回答by datha
回答by Aaron Brager
If
如果
- your product's normal targets run fine, but
- you get the error when running your test targets…
- 您产品的正常目标运行良好,但是
- 运行测试目标时出现错误...
note that each target uses (and should use) a different filename for the Objective-C Generated Interface Header Name.
请注意,每个目标为 Objective-C Generated Interface Header Name 使用(并且应该使用)不同的文件名。
This means you cannot import the generated header file in your Objective-C .h
files, because they won't be found in the test target:
这意味着您不能在 Objective-C.h
文件中导入生成的头文件,因为它们不会在测试目标中找到:
Instead, you should move these #import
statements into your Objective-C .m
(implementation files), where they'll build successfully.
相反,您应该将这些#import
语句移动到您的 Objective-C .m
(实现文件)中,在那里它们将成功构建。
If you need to refer to Swift classes in a .h
file, use the @class
directive, e.g.:
如果您需要在.h
文件中引用 Swift 类,请使用@class
指令,例如:
//
// ViewController.h
// import-example
//
#import <UIKit/UIKit.h>
@class SomeSwiftClass;
@interface ViewController : UIViewController
- (NSString *) titleFromGenerator:(SomeSwiftClass *)generator;
@end
回答by Tal Zion
You need to add this in your build settings.
您需要在构建设置中添加它。
There might be an issue while migrating to Xcode 8, where you will have unspecified
in the build setting Swift header file.
迁移到 Xcode 8 时可能会出现问题,您将unspecified
在构建设置中包含 Swift 头文件。
This if from killerz
这如果来自killerz
Go to Build Settings->Objective-C Generated Interface Header Name and set the value to YourModule-Swift.h (this is usually already set, this is the filename you need to import on .m file #import "YourModule-Swift.h"
转到 Build Settings->Objective-C Generated Interface Header Name 并将值设置为 YourModule-Swift.h(这通常已经设置,这是您需要在 .m 文件中导入的文件名 #import "YourModule-Swift.h ”