2015-11-05 20 views
0

有關如何從Swift和Objective-C調用C++的一些很好的答案。例如his answer to "Can I have Swift, Objective-C, C and C++ files in the same Xcode project?" SwiftArchitect展示瞭如何從Swift中調用C,C++,Objective-C,Objective-C++和Swift。使用另一個C++類作爲參數調用Objective-C(++)的C++方法

但是我可以在例子中找到的C++方法的簽名非常簡單。我想調用一些將其他C++類作爲輸入和輸出參數的C++方法。讓我舉一個虛擬的例子。這裏有兩個C++類。

class CPPClassA 
{ 
public: 
    int myState; 
    int doSomethingAmazingWithTwoInts(int firstInt, int secondInt); 
}; 

class CPPClassB 
{ 
public: 
    int doSomethingAmazingWithTwoClassAObjects(CPPClassA &firstObject, CPPClassA *secondObject); 
}; 

如果我想從斯威夫特稱CPPClassB::doSomethingAmazingWithTwoClassAObjects我怎麼在這兩個CPPClassA情況下掠過我的Objective-C++包裝我CPPClassB類?

回答

-1

最簡單的方法是創建ObjC包裝。

//ObjCClassA.h 
@interface ObjCClassA 

@property (nonatomic, assign) int myState; 

- (int) doSomethingAmazingWithFirstInt:(int) firstInt secondInt:(int) secondInt; 

@end 

//ObjCClassA.mm 
@interface ObjCClassA() 
{ 
    CPPClassA val; 
} 
@end 

@implementation 

- (int) myState 
{ 
    return val.myState; 
} 

- (void) setMyState:(int) myState 
{ 
    val.myState = myState; 
} 

- (int) doSomethingAmazingWithFirstInt:(int) firstInt secondInt:(int) secondInt 
{ 
    return val.doSomethingAmazingWithTwoInts(firstInt, secondInt); 
} 

@end 

//ObjCClassB.h 
@class ObjCClassA; 

@interface ObjCClassB 

- (int) doSomethingAmazingWithFisrtA:(ObjCClassA*) firstA secondA:(ObjCClassB*) secondB; 

@end; 

最難的方法是使用C包裝。最難的,因爲你需要手動執行內存管理。

相關問題