0

是否可以在不将每个 .m 文件更改为 .mm 的情况下执行此操作?

好的。我正在尝试实施答案,但遇到了麻烦。看看下面我的 Objective C++ .h 和 .mm

Objective-C++ - IDCaptureTemplateCommand.h:

#include "Template.h"

@interface IDCaptureTemplateCommand : NSObject
@property (nonatomic, strong) IDCaptureTemplateCommand *IDCaptureTemplateCommand;

@end

Objective-C++ - IDCaptureTemplateCommand.mm:

#include "IDCaptureTemplateCommand.h"

@implementation IDCaptureTemplateCommand

- (id)init
{
    self = [super init];
    if (self) {
        self.captureTemplateCommand = [[IDCaptureTemplateCommand alloc] init];
    }
    return self;
}

@end

Objective-C - IDCameraViewController.h

#import <UIKit/UIKit.h>
#import "IDCaptureTemplateCommand.h"

@interface IDCameraViewController : UIViewController <UINavigationControllerDelegate>

@property (nonatomic) IDCaptureTemplateCommand *captureCommand;  //ERROR - Unknown type name 'IDCaptureTemplateCommand'

@end
4

1 回答 1

3

您可以像在 C 中使用 C++ 或其他方式一样,这样做。您需要能够使用纯 Objective-C 声明接口,然后可以使用 Objective-C++ 编写实现。

如果您的头文件使用 C++,例如您的类有一个std::string实例变量,那么为了使该功能可以从 Objective-C 访问,您必须编写一个包装器或以其他方式在接口处隐藏 C++,这样您的 Objective-C 文件就不会需要查看任何 C++ 声明。

于 2013-10-09T23:09:40.387 回答