Importing the automatically generated -Swift.h into an ObjC++ file (where modules are not enabled) can lead to errors because imports made in Swift are only exposed as @import statements.
Example:
The following code is part of a framework TestFramework
and has a class that uses a type from AppKit.
import AppKit
@objc public class TestClass: NSObject {
@objc public var state: NSControl.StateValue = .off
}
The interface header re-exports the AppKit import like this, but there is no else case for !__has_feature(objc_modules)
:
#if defined(__OBJC__)
#if __has_feature(objc_modules)
#if __has_warning("-Watimport-in-framework-header")
#pragma clang diagnostic ignored "-Watimport-in-framework-header"
#endif
@import AppKit;
@import ObjectiveC;
#endif
#endif
…
SWIFT_CLASS("_TtC13TestFramework9TestClass")
@interface TestClass : NSObject
@property (nonatomic) NSControlStateValue state;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end
Importing <TestFramework/TestFramework-Swift.h>
in an ObjC++ file will fail, since it can't resolve NSControlStateValue
.
Now it is possible to fix it by adding #import <AppKit/AppKit.h>
before #import <TestFramework/TestFramework-Swift.h>
but that is very fragile and inconvenient.
I had a look into the swift compiler sources and also played around with the -emit-clang-header-nonmodular-includes
flag, but that did not help.
Is this expected behavior or is there a way to configure the creation of the interface header to also generate #import
statements?
Any help would be greatly appreciated!
Markus