【发布时间】:2016-06-24 15:24:15
【问题描述】:
我正在尝试修改 C++ 类,使 Objective C 对象成为其实例变量之一。
本质上,这个问题就像这个问题的反面:C++ classes as instance variables of an Objective-C class
头文件MyCPPClass.h如下所示:
namespace my_namespace{
class MyCPPClass: public my_namespace::OperationHandler {
public:
void someFunc();
private:
void otherFunc();
};
}
MyCPPClass.mm 文件如下所示:
#include "MyCPPClass.h"
#import <Foundation/Foundation.h>
void my_namespace:: MyCPPClass:: someFunc() {
NSLog(@"I can run ObjC here! Yahoo!");
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"download_url_goes_here"]];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// handle the data
}];
}
void my_namespace:: MyCPPClass:: otherFunc() {
NSLog(@"I can run ObjC here! Yahoo!");
}
这里的问题是我需要存储对在someFunc() 中创建的对象的引用,然后在otherFunc() 中使用它。该对象是NSURLSessionTask 的一个实例。我在 C++ 方面的背景非常有限 :(。最不打扰的方法是什么?
【问题讨论】:
标签: c++ ios objective-c class objective-c++