尝试通过forwardInvocation获取NSDate

我有个问题。

我有方法

- (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 

但是对于另一个types的对象来说, 我如何解决这个问题? 谢谢

obj可以在赋值之后立即释放,因为这是一个unsafe_unretained引用,所以-setReturnValue:将悬挂指针作为参数。

如果unsafe_unretained是不可避免的setterpath,(因为-[NSInvocation getArgument:atIndex:]可能无法正常使用强引用,谢谢@Tommy注意到这一点),你可以不同的方式处理getterpath,强引用。