Calling C++ functions using Objective-C++ in a Swift program

I've imported a 'TestCPP.cpp' file into my Swift project and wrote a wrapper for the formula I want to call from a Swift program. The program compiles just fine and within 'main.swift' the formula autocompletes and I can see the definition properly under "quick help"

The problem is I get the error "Extra argument in call." To try and backtrack the error I started going parameter by parameter and get errors that say "Cannot convert value of type 'Double' to expected argument type 'TestObjC' ". This doesn't make sense as I built 9 parameters, the first 6 are doubles and last 3 are strings and that's what I've input into the function in the swift file.


Anyone have any idea why I'm getting these errors and how to fix them?




main.swift

let test = TestObjC.TestCPP(200.00,210.00,0.25,0.15,0.0025,0.02,"alpha","bravo","charlie")


Bridging Header file

#import "TestObjC.h"

TestObjC.h

#import <Foundation/Foundation.h>
@interface TestObjC : NSObject
- (double) TestCPP: (double) A : (double) B : (double) C :(double) D : (double) E : (double) F : (NSString*) Caller : (NSString*) Sector : (NSString*) OutPut;
@end

TestObjC.mm


#import “TestObjC.h”
#import “TestCPP.h"

@interface TestObjC(){
    Test wrapped;
}
@end

NSString *Caller  = Caller;
string Call = [Caller UTF8String];
NSString *Sector = Sector;
string Sect = [Sector UTF8String];
NSString *OutPut  = OutPut;
string OP = [OutPut UTF8String];

@implementation TestObjC
- (double) TestCPP: (double) A : (double) B : (double) C :(double) D : (double) E : (double) F : (NSString*) Caller : (NSString*) Sector : (NSString*) OutPut;
@end
    return wrapped.TesterFunction((double) A, (double) B, (double) C, (double)D, (double)E, (double) F, Call, Sect,OP);
}
@end

Hi,


The error makes sense due to the fact that you are trying to call a _class_ method when the declaration of that method is an instance method.

Try changing

- (double) TestCPP: (double) A : (double) B : (double) C :(double) D : (double) E : (double) F : (NSString*) Caller : (NSString*) Sector : (NSString*) OutPut;

to

+ (double) TestCPP: (double) A : (double) B : (double) C :(double) D : (double) E : (double) F : (NSString*) Caller : (NSString*) Sector : (NSString*) OutPut;


My other suggestion is to have an object that will represent each field within its interface. In this case you'll avoid such a big method signature, it will especially reduce complexity for the reader.


Another way of doing this is, of course, create an instance of the class and call that method, like this:

let test = TestObjC().TestCPP(200.00,210.00,0.25,0.15,0.0025,0.02,"alpha","bravo","charlie")


First part (TestObjc()) will create an intsance (call initializer & allocate memory), and the second part is simply calling an instance method.

Thanks!


I utilized your suggestion for creating an instance and it built successfully!


let test = TestObjC().TestCPP(200.00,210.00,0.25,0.15,0.0025,0.02,"alpha","bravo","charlie")


The major headache I've had is in trying to figure out the best way to convert NSStrings from Objective-C to std::strings in C++. My background is in C++ and Objective C is new to me. I utilized the below code in my .mm file to keep the header clean of C++ code. The problem now is that when I run the project the compiler is throwing EXC_BAD_ACCESS issues. I'm assuming it has to do with the NSStrings* pointers I called in the header file.


NSString *Caller  = Caller;
string Call = [Caller UTF8String];
NSString *Sector = Sector;
string Sect = [Sector UTF8String];
NSString *OutPut  = OutPut;
string OP = [OutPut UTF8String];


Has anyone figured out an easier way to do this? I like the idea of building libraries and functions in C++ and coming up with easy wrappers to port them into Swift for use in OS/iOS etc.

Calling C&#43;&#43; functions using Objective-C&#43;&#43; in a Swift program
 
 
Q