c++ - 从 Objective-C 调用 C++

标签 c++ ios objective-c objective-c++

各位已经成功从Objective-C调用C++代码的 friend ,请赐教?

这个 link说你需要使用他在他的文章中描述的技术来包装你的 C++ 代码。看起来不错,但我还是有问题。

这个 link表示只要调用 C++ 类的 Objective-C 类已转换为 .mm (Objective-C++) 类,那么两者应该可以很好地协同工作。

当我尝试添加方法调用时,这些方法中的每一种都让我感到悲伤。谁能给我一个简单的 Hello World iOS 应用程序的代码,该应用程序使用 Objective-C 作为“Hello”部分,使用 C++ 类作为“World”部分,中间有一个 Objective-C++ 类?还是我的整个概念都错了?

最佳答案

本质上,您需要一个扩展名为 .mm 的 ObjC 类,它调用扩展名为 .mm 的 ObjC 类。第二个将用作 C++ 包装类。包装类将调用您实际的 .cpp 类。这有点棘手,所以我会给你一些详细的代码。以下是该项目的概述:

enter image description here

在您的 ObjC 代码 (ViewController) 中,您将调用 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

#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

#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

#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;
}

这段代码并不完美!我在识别命名空间测试时遇到问题。有空我会更新的。

但这应该能让你到达那里!!!!

关于c++ - 从 Objective-C 调用 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19229574/

相关文章:

C++, Linux : error: conversion from ‘boost::unique_future<void>’ to non-scalar type ‘boost::shared_future<void>’ requested. 如何绕过它?

iphone - cocos2d如何组合 Sprite

objective-c - 当我不知道 UITableView 有多少个部分时,如何重新加载 UITableView 的数据?

c++ - Qt : Setting a border bottom on QtableView disables selection-background-color

c++ - 将没有名称的变量传递给函数

ios - 无法以编程方式设置 UINavigationControllerDelegate(但可以通过 Storyboard设置)

ios - 我可以在不创建 UILabel 的情况下计算出 NSString 的 numberOfLines 吗?

objective-c - 在 Objective-C 中获取文件的大小

c++ - Libtorrent - 给定一个磁力链接,你如何生成一个 torrent 文件?

ios - 在Phonegap/Cordova中使用Durandal