objective-c - oc, c++混编,c++函数中如何识别self?
本文介绍了objective-c - oc, c++混编,c++函数中如何识别self?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
一个项目,用到了opencv库,有一个代理方法,这里假设为void cPlusPlusMethod(),在这个函数中要调用[self test1];但是在这个函数中无法识别self.如何解决?
以下是代码:
#import "ViewController.h"
#include <fstream>
using namespace std;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
void cPlusPlusMethod() {
//在C++这个代理方法中 ,要运行 用oc写的类方法:[self test1];
[self test1];//不识别self如何解决?
}
- (void) test1 {
NSLog(@"test1 ");
}
@end
解决方案
那不叫代理方法,应该叫回调函数。
C的函数运行时都是放在内存的代码区,所以要想访问OC的对象,需要参数传递或访问全局变量。
#import "ViewController.h"
#include <fstream>
using namespace std;
static ViewController *handle;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
handle = self;
}
void cPlusPlusMethod() {
[handle test1];
}
- (void)test1 {
NSLog(@"test1 ");
}
- (void)dealloc {
handle = NULL;
}
@end
这篇关于objective-c - oc, c++混编,c++函数中如何识别self?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文