我试图从类中获取OpenGL ES本机窗口(_win):
@interface CAEAGLLayer : CALayer <EAGLDrawable>
{
@private
struct _CAEAGLNativeWindow *_win;
}因此,我将其扩展为类别:
@interface CAEAGLLayer(MyLayer)
- (void*) fetchWin;
@end
@implementation CAEAGLLayer(MyLayer)
- (void*) fetchWin
{
return self->_win;
}
@end并在另一个类中使用:
@implementation MyClass
- (void)setupLayer
{
_eaglLayer = (CAEAGLLayer*)self.layer;
_eaglLayer.opaque = YES;
NSLog(@"_eaglLayer _win: %p", [_eaglLayer fetchWin]);
}
@end 但是在构建时,遇到了一个链接错误:
Undefined symbols for architecture x86_64:
"_OBJC_IVAR_$_CAEAGLLayer._win", referenced from:
-[CAEAGLLayer(MyLayer) fetchWin] in OpenGLView.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)发布于 2014-11-03 05:44:36
链接器找不到这个符号,因为默认情况下,"ivar符号代表@private和@package ivars 不出口“。因此,您不能以这种方式直接按名称访问_win,即使您有一个引用它的头。
但是,您可以深入ObjC运行时并提取实例变量。在您的示例中,您可以在-setupLayer中尝试类似的操作(在#导入<objc/objc-runtime.h>之后):
Ivar winIvar = class_getInstanceVariable([CAEAGLLayer class], "_win");
void * winptr = (__bridge void *)object_getIvar(_eaglLayer, winIvar);(您还可以在层上使用一个简单的-valueForKey:,使用@"_win"作为键的名称,但我更喜欢运行时方法,因为它们对您要做的事情阅读得更清楚,这基本上绕过了预期的抽象。)
https://stackoverflow.com/questions/26706967
复制相似问题