我有一个问题。
我有方法
- (void)forwardInvocation:(NSInvocation *)anInvocation {
SEL sel = anInvocation.selector;
NSString *prop = [self.class fieldNameForSelector:sel];
if (prop) {
BOOL setter = [self isSetter:sel];
__unsafe_unretained id obj;
if (setter) {
[anInvocation getArgument:&obj atIndex:2];
[self setValue:obj forKey:prop];
} else {
obj = [self valueForKey:prop];
[anInvocation setReturnValue:&obj];
}
} else {
[super forwardInvocation:anInvocation];
}
}
但是,如果我尝试获取对象类NSDate或NSData,它在64位设备中不起作用。我得到EXC_BAD_ACCESS code=1
来自NSZombie的消息
[__NSDate retain]: message sent to deallocated instance 0x171e00d50
但对于其他类型的对象,它可以工作。我怎么解决这个问题?谢谢
答案 0 :(得分:4)
obj
可以在分配后立即解除分配,因为它是unsafe_unretained
引用,因此-setReturnValue:
将悬空指针作为参数。
如果unsafe_unretained
对于setter路径是不可避免的,(因为-[NSInvocation getArgument:atIndex:]
可能无法正确使用强引用,感谢@Tommy注意到这一点),您可以通过强引用以不同的方式处理getter路径。 / p>