Search this community site, there are many answers that address this problem. See, for example, this: How to use Superpowered lib in Swift project
The specific error message in the image is due to the fact that getDeviceInformation()
returns a reference to IParameters
, which is assigned to an incompatible type OParameters*
, i.e. a pointer to OParameters
. The code of OParameters
is not given, but I would guess that converting an IParameters
reference to an OParameters
pointer would be tricky and unnecessary. Anyway, your OParameters
class evidently does not provide such a conversion.
As you can see in the aforementioned answer, it is impossible to expose C++ code to Swift directly, and IParameters
is a C++ class. Objective-C++ allows you to mix Objective-C and C++, but only the Objective-C portion of the code can be exposed to Swift through headers. So, you will need to create an Objective-C class that is populated based on the contents of an IParameters
instance and returned to Swift. In addition, if you want changes made in Swift to be visible in C++, you would need to keep a pointer or a reference to IParameters
in the wrapper .mm
file, but NOT in any headers visible to Swift, and have setters, visible to Swift, that can be used to modify the IParameters
state.
Here is a quick and dirty example. The focus here is only on sharing IParams
between C++ and Swift. Things like memory management are out of scope here. For simplicity, let us say our IParams
and EMC
C++ classes look as follows:
class IParams {
public:
int32_t param1;
int32_t param2;
};
class ECM
{
public:
static ECM * getInstance();
// It's very important that a reference is returned here. This
// allows us to store the pointer to the referenced IParams in a
// member of OParams, and the OParams' getters and setters can then
// directly access the IParams object returned here by reference.
IParams& getDeviceInfo();
...
};
Here are their wrapper implementations in Objective-C++ (.mm) code, which refer to C++ code directly, but it's OK as this is hidden from Swift:
@implementation OParams
{
IParams * pIParams;
}
-(id)init:(IParams*)pip
{
pIParams = pip;
return self;
}
// These getters and setters directly access the memory of the IParams
// object pointed to by pIParams.
-(int32_t)getParam1 { return pIParams->param1; }
-(int32_t)getParam2 { return pIParams->param2; }
-(void)setParam1:(int32_t)p { pIParams->param1 = p; }
-(void)setParam2:(int32_t)p { pIParams->param2 = p; }
@end
@implementation ECMWrapper
-(OParams*)getDeviceInfo
{
// We're returning a pointer to an OParams instance whose pIParams
// member points to the IParams object the reference to which is
// returned from ECM::getDeviceInfo()
return [[OParams alloc] init:(&ECM::getInstance()->getDeviceInfo())];
}
@end
Finally, here are the declarations that are visible to Swift, directly or indirectly, via the bridging header:
@interface OParams : NSObject
-(int32_t)getParam1;
-(int32_t)getParam2;
-(void)setParam1:(int32_t)p;
-(void)setParam2:(int32_t)p;
@end
@interface ECMWrapper : NSObject
-(OParams*)getDeviceInfo;
@end
Note that no C++ types or methods are mentioned here, so this code can be used in Swift.