从Objective-C调用C ++ [英] Calling C++ from Objective-C
问题描述
对于那些已经能够成功从Objective-C调用C ++代码的人,请给我启发?
For those of you who have successfully been able to call C++ code from Objective-C, will you please enlighten me?
此链接表示您需要使用他在文章中描述的技术来包装C ++代码.看起来不错,但我仍然有问题.
This link says you need to wrap your C++ code using a technique he describes in his article. It looks good but I still have problems.
此链接说只要将调用C ++类的Objective-C类转换为.mm(Objective-C ++)类,那么这两个类就可以很好地协同工作.
This link says that as long as the Objective-C class calling the C++ class has been converted to a .mm (Objective-C++) class then the two should work nicely together.
这些方法中的每一种都使我在尝试添加方法调用的那一刻感到悲痛.有人可以给我提供一个简单的Hello World iOS应用程序的代码,该应用程序将Objective-C用于"Hello"部分,将C ++类用于"World"部分,中间使用Objective-C ++类吗?还是我仍然对整个概念有误?
Each of these approaches are causing me grief the minute I try to add a method call. Can somebody please give me code for a simple Hello World iOS app that uses Objective-C for the "Hello" part and a C++ class for the "World" part with an Objective-C++ class in the middle? Or do I still have the entire concept wrong?
推荐答案
基本上,您需要一个扩展名为.mm的ObjC类,并调用一个扩展名为.mm的ObjC类.第二个将用作C ++包装器类.包装器类将调用您的实际.cpp类.这有点棘手,所以我将给您一些冗长的代码.这是该项目的概述:
Essentially you need an ObjC class with .mm extension that calls an ObjC class with .mm extension. The second one will be used as a C++ wrapper class. The wrapper class will call your actual .cpp class. It's a little tricky, so I'm going to give you some verbose code. Here is an overview of the project:
在您的ObjC代码(ViewController)中,您将调用CplusplusMMClass
In your ObjC code (ViewController) you would call the CplusplusMMClass
- (IBAction)buttonPushed:(UIButton *)sender {
self.mmclass = [[CplusplusMMClass alloc]init]; // bad practice; but showing code
NSString *str = [self.mmclass fetchStringFromCplusplus];
[self populateLabel:str];
}
这是CplusplusMMClass .h和.mm
Here is the CplusplusMMClass .h and .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
这里是WrapperClass .h和.mm
Here is WrapperClass .h and .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
这是PureCplusplusClass .h和.cpp
Here is the PureCplusplusClass .h and .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 is not perfect! I'm having trouble with the namespace test being recognized. I'll update when I can.
这篇关于从Objective-C调用C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!