我在私有(private)框架中的函数使用的对象的实现中实现了一个 C 回调,因此我无法控制传递给回调的参数。我想在回调中使用 self
。谢谢。
编辑:
MyObject.h/.m
#import "PrivateFramework.h"
@interface MyObject : NSObject
-(void) start;
@end
void callback(void* arg1, int arg2);
void callback(void* arg1, int arg2) {
/*
here I would like to use self (current instance of the object) but
I can't pass a pointer to the instance in the callback since I don't
control what is passed to it.
*/
@implementation MyObject
-(void)start {
// this function is inside a private framework.
PrivateFunction(&callback);
}
@end
最佳答案
几个选项:
- 使用单例来管理流程
添加一个“类”变量(文件级静态)来保存请求实例
static MyObject *callbackResponder = nil;
这可能会在 .m 文件中的 #imports 和 @implementation 之间进行。
关于objective-c - 如何从 C 函数获取对象的当前实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16240917/