简体   繁体   English

从桥接头中排除 Swift 类

[英]Exclude Swift Class from Bridging Header

In a Swift project that has mixed Obj-C and C++, I have a class that extends the class belonging to a 3rd party framework.在一个混合了 Obj-C 和 C++ 的 Swift 项目中,我有一个类扩展了属于 3rd 方框架的类。 When I compile the project, the compiler complains that it Cannot find interface declaration for '<Framework Class Name>', superclass of '<My Subclass Name>'当我编译项目时,编译器抱怨它Cannot find interface declaration for '<Framework Class Name>', superclass of '<My Subclass Name>'

The semantic error points to the auto-generated bridging header ( 'MyProjectName-Swift.h' ).语义错误指向自动生成的桥接头 ( 'MyProjectName-Swift.h' )。

Is there a way to not have a particular class included in the bridging header besides marking it private or fileprivate?除了将其标记为私有或文件私有之外,有没有办法在桥接头中包含特定类? (Which makes the subclass not much use in the rest of the project..) I've looked through the Apple docs on this matter and there doesn't seem to be any specific direction on this. (这使得该子类在项目的其余部分中没有多大用处。。)我已经查看了关于这个问题的 Apple 文档,似乎没有任何具体的方向。

Alternatively, any clues as to how to fix this?或者,有关如何解决此问题的任何线索? The header includes this bit:标头包括以下位:

#if __has_feature(modules)
@import UIKit;
@import Material;
@import CoreGraphics;
@import ObjectiveC;
#endif

Which seems like it should make the proper reference to the superclass (in this case, if it matters, Material.PresenterCard ) But — I'm pretty sure this pre-compiler directive isn't being referred to as I've heard of a related bug.这似乎应该正确引用超类(在这种情况下,如果重要的话, Material.PresenterCard )但是 - 我很确定这个预编译器指令没有被引用,因为我听说过相关的错误。

I recently had a very similar problem and solved it by defining an empty Objective-C class for the case where modules weren't available.我最近遇到了一个非常相似的问题,并通过为模块不可用的情况定义一个空的 Objective-C 类来解决它。

My situation was App -> Framework -> Package (Swift Package)我的情况是 App -> Framework -> Package (Swift Package)

Framework implemented DerivedClass , which subclassed BaseClass defined in Package Framework实现DerivedClass ,它继承了Package中定义的BaseClass

When importing Framework-Swift.h internally or externally within any Objective-C++ code, the compiler would complain that BaseClass could not be found in the generated header在任何 Objective-C++ 代码内部或外部导入Framework-Swift.h时,编译器会抱怨在生成的标头中找不到BaseClass

I solved this by adding the following code to Framework's umbrella include (Framework.h):我通过将以下代码添加到框架的伞包括(Framework.h)中解决了这个问题:

#if !__has_feature(modules)
@interface BaseClass: NSObject
@end
#endif

So when modules are unavailable (Objective-C++), BaseClass is defined as an empty Objective-C class.所以当模块不可用时(Objective-C++),BaseClass 被定义为一个空的 Objective-C 类。 This allowed the framework to be imported into Objective-C++ code to use other features of the framework这允许将框架导入到 Objective-C++ 代码中以使用框架的其他功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM