1 year ago

#331642

test-img

P. Tsin

How to bridge Objective-C++ code to Swift when we have C++ classes as parameters in header files?

I'm trying to call C++ methods from Swift, and what I've learnt is that I can wrap the C++ stuff to an Objective-C++ class first, then export this Objective-C++ class to Swift. Here's my code:

// CPPClass.h
class CPPClass {
    int data;
};


// OCClass.h
#import "CPPClass.h"
@interface OCClass : NSObject

- (instancetype)initWithCPPObject:(CPPClass &)obj;

@end


// MyProject-Bridging-Header.h
#import "OCClass.h"

When I build the project, the compiler throws an Expected a type error in OCClass.h. If I comment out the header file in MyProject-Bridging-Header.h, everything becomes fine, except that I can't use it in Swift any more.

I can solve it by replacing the specific C++ type parameter with a void * pointer, and in this case I can freely use CPPClass in OCClass.mm file, but I wonder if there's any more elegant solutions?

ios

swift

objective-c

objective-c++

0 Answers

Your Answer

Accepted video resources