Exclude Swift Class from Bridging Header
Asked Answered
T

1

7

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.

Truncate answered 9/12, 2017 at 22:26 Comment(3)
Is the superclass from the library an Objective-C class?Any
@JoshCaswell No — it's a Swift class.Truncate
got the same problem did you find a solution ?Grandsire
B
0

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

Brian answered 10/7, 2022 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.