尝试通过forwardInvocation获取NSDate [英] Try get NSDate through forwardInvocation

查看:72
本文介绍了尝试通过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

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

还有来自NSZombie的消息

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

推荐答案

obj可以在分配后立即取消分配,因为它是一个unsafe_unretained引用,因此-setReturnValue:可以获取悬空指针作为参数.

obj could deallocate right after assignment, because it's an unsafe_unretained reference, so -setReturnValue: gets dangling pointer as an argument.

如果unsafe_unretained对于设置程序路径是不可避免的(由于-[NSInvocation getArgument:atIndex:]在强引用下可能无法正常工作,感谢@Tommy指出这一点),则可以在强引用下以不同方式处理getter路径.

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.

这篇关于尝试通过forwardInvocation获取NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆