标头:
@interface DisplayConnection : NSObject
{
UIView *_view;
struct UnityDisplaySurfaceBase *_surface;
}
@property(readonly, nonatomic) struct UnityDisplaySurfaceBase *surface;
@property(readonly, copy, nonatomic) UIView *view;
@end
我可以得到_view的值:
id value = object_getIvar(instance, _viewIvar);
但是在
object_getIvar(instance, _surfaceIvar)
时崩溃,并且无法捕获异常。帮助,谢谢。
最佳答案
我只是解决了这个问题。该解决方案适用于C基本类型/结构。
id不能指向struct。
@interface DisplayConnection : NSObject
{
UIView *_view;
struct UnityDisplaySurfaceBase _surface;//without pointer value
}
@property(readonly, nonatomic) struct UnityDisplaySurfaceBase surface;
@property(readonly, copy, nonatomic) UIView *view;
@end
@implementation DisplayConnection
- (UnityDisplaySurfaceBase)someValue {
Ivar inner = class_getInstanceVariable ([self class], "_surface");
ptrdiff_t offset = ivar_getOffset(inner);
unsigned char *stuffBytes = (unsigned char *)(__bridge void *)self;
UnityDisplaySurfaceBase someVar = * ((UnityDisplaySurfaceBase *)(stuffBytes + offset));
return someVar;
}
@end
参考:https://stackoverflow.com
参考:https://www.bignerdranch.com