Swift extensions in Objective-C(++)?

I was hoping to define a class in Swift (descended from NSObject) and write a handful of methods in a separate .mm (Objective-C++) file, but I can't figure out how to do that. Should it be a category? I can't declare the method in a header file to include in the Bridging Header, because that can't see the Module-Swift.h autogenerated interop header.

Not sure if this is possible. Any suggestions? Thanks!

Could the NSObject subclass be defined in Objective-C++ rather than Swift?

  • it could either be an "abstract" class which is further subclassed in Swift;
  • or the Swift APIs could be added in extensions (without stored properties).
1 Like

You can probably still declare the methods in a file included in the bridging header. Instead of importing Module-Swift.h in the header, use a forward declaration (@class MyClassName;) in the header, and import Module-Swift.h in the implementation file.

1 Like

I could certainly define it in Obj-C++, I had just hoped to do it in Swift with only a couple of methods in Obj-C++.

I tried putting a forward declaration, but it doesn't like that.

@class RebuildMinimapViewController;

@interface RebuildMinimapViewController (CPP)

- (void)	deleteCacheFiles;

RebuildMinimapViewController+CPP.h:14:12: Cannot define category for undefined class 'RebuildMinimapViewController'

I'll just declare the class in Obj-C++.