devios1
devios1

Reputation: 38035

Auto-generated "ModuleName-Swift.h" header not including Swift classes

I am trying to reference some Swift-defined classes from my Objective-C implementation file, but for some reason, though I've gotten the header file to auto-generate, it doesn't appear to be including any information about the Swift classes in the project.

My Swift class is attributed with @objc yet even after importing the "-Swift.h" file, I still get a "Use of undeclared identifier" error when compiling.

I can't figure out what I'm missing. I have Defines Modules set to YES in the project.

Also of note: if I command-click the symbol from my Obj-C file, Xcode successfully finds the definition in the Swift file.

Upvotes: 3

Views: 1989

Answers (2)

gheclipse
gheclipse

Reputation: 1009

You may need to derive from NSObject.

I encountered a similar situation where some Swift classes were not being exposed to Obj-C through the auto generated header. The solution is to derive Swift classes from NSObject.

class SwiftClassNotInHeader { }
class SwiftClassInHeader : NSObject { }

From MyApp-Swift.h

SWIFT_CLASS("_TtC6Server18SwiftClassInHeader")
@interface SwiftClassInHeader : NSObject
- (SWIFT_NULLABILITY(nonnull) instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

SwiftClassNotInHeader is not in myApp-Swift.h

Upvotes: 1

z33
z33

Reputation: 1263

Make sure in build setting you have got this setup:

Objective-C Bridging Header : $(SRCROOT)/Sources/SwiftBridging.h

Sometime when you import a swift file directly Xcode don't prompt you to add a bridging header. it's a must have step even you don't call objective-c from swift.

Upvotes: 2

Related Questions