ios - Calling C++ from Objective-C -
for of have been able call c++ code objective-c, please enlighten me?
this link says need wrap c++ code using technique describes in article. looks still have problems.
this link says long objective-c class calling c++ class has been converted .mm (objective-c++) class 2 should work nicely together.
each of these approaches causing me grief minute try add method call. can please give me code simple hello world ios app uses objective-c "hello" part , c++ class "world" part objective-c++ class in middle? or still have entire concept wrong?
essentially need objc class .mm extension calls objc class .mm extension. second 1 used c++ wrapper class. wrapper class call actual .cpp class. it's little tricky, i'm going give verbose code. here overview of project:

in objc code (viewcontroller) call cplusplusmmclass
- (ibaction)buttonpushed:(uibutton *)sender { self.mmclass = [[cplusplusmmclass alloc]init]; // bad practice; showing code nsstring *str = [self.mmclass fetchstringfromcplusplus]; [self populatelabel:str]; } here cplusplusmmclass .h , .mm
#import <foundation/foundation.h> #import "wrapperclass.h" @interface cplusplusmmclass : nsobject @end @interface cplusplusmmclass() @property (nonatomic, strong) wrapperclass *wrapper; - (nsstring*)fetchstringfromcplusplus; @end #import "cplusplusmmclass.h" #import "wrapperclass.h" @implementation cplusplusmmclass - (nsstring*)fetchstringfromcplusplus { self.wrapper = [[wrapperclass alloc] init]; nsstring * result = [self.wrapper gethellostring]; return result; } @end here wrapperclass .h , .mm
#ifndef headerfile_h #define headerfile_h #import <foundation/foundation.h> #if __cplusplus #include "purecplusplusclass.h" @interface wrapperclass : nsobject @end @interface wrapperclass () - (nsstring *)gethellostring; @end #endif #endif #import "wrapperclass.h" #include "wrapperclass.h" #include "purecplusplusclass.h" using namespace test; @interface wrapperclass () @property (nonatomic) hellotest hellotest; @end @implementation wrapperclass - (nsstring *)gethellostring { self.hellotest = *(new hellotest); std::string str = self.hellotest.gethellostring(); nsstring* result = [[nsstring alloc] initwithutf8string:str.c_str()]; return result; } @end here purecplusplusclass .h , .cpp
#ifndef __helloworld__purecplusplusclass__ #define __helloworld__purecplusplusclass__ #include <stdio.h> #include <string> using namespace std; namespace test { class hellotest { public: std::string gethellostring(); }; } #endif /* defined(__helloworld__purecplusplusclass__) */ #include <stdio.h> #include <string> std::string test::hellotest::gethellostring() { std::string outstring = "hello world"; return outstring; } this code not perfect! i'm having trouble namespace test being recognized. i'll update when can.
Comments
Post a Comment