从类中访问私有实例方法



我有一个没有在头文件(private(中声明的实例方法,我仍然想使用该类中的任何其他实例方法,例如

具有公共方法的标头

@interface Tap : NSObject
- (void)enable;
- (void)disable;
@end

Impl 类

@implementation Tap
- (CGEventRef)callback:(CGEventTapProxy)proxy :(CGEventType)type :(CGEventRef)event :(void *)context {
...
}
- (void)enable {
CGEventTapCreate(tap, place, options, mask, /* How to pass the callback here? */, context);
}
...
@end

我似乎不能只使用self.callback之类的东西,那么我如何将此方法传递给EventTap呢?

在实现中混合C没有问题

CGEventRef eventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {

// when userinfo is used as reference to the object
Tap *tap = (__bridge Tap *)refcon;
[tap exampleMethod];
// your logic here ..

return event;
}
- (void)enable {
void *userinfo = (__bridge void *)(self);
CGEventTapCreate(tap, place, options, mask, (CGEventTapCallBack)eventCallback, userinfo);
}

最新更新