简体   繁体   中英

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. When I compile the project, the compiler complains that it 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' ).

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.

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.

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.

My situation was App -> Framework -> Package (Swift Package)

Framework implemented DerivedClass , which subclassed BaseClass defined in Package

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

I solved this by adding the following code to Framework's umbrella include (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. This allowed the framework to be imported into Objective-C++ code to use other features of the framework

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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