问题
I have a problem.
I have method
- (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];
}
}
But if I try to get object class NSDate or NSData it's doesn't work in 64-bit device. I get EXC_BAD_ACCESS code=1
And message from NSZombie
[__NSDate retain]: message sent to deallocated instance 0x171e00d50
But for another type object it works. How I can resolved this problem? Thank you
回答1:
obj
could deallocate right after assignment, because it's an unsafe_unretained
reference, so -setReturnValue:
gets dangling pointer as an argument.
If unsafe_unretained
is unavoidable for setter path, (since -[NSInvocation getArgument:atIndex:]
may not work properly with strong reference, thanks @Tommy for noting this), you could handle getter path differently, with strong reference.
来源:https://stackoverflow.com/questions/25009990/try-get-nsdate-through-forwardinvocation