我正在使用NSInvocation
并且需要从中检索属性之一。
我正在使用以下代码,但是我有一些奇怪的行为调用[invocation invoke];
:
NSString *propertyName = nil;
[invocation getArgument:&propertyName atIndex:3];
我读到为了使其在ARC下工作,我们需要使用__unsafe_unretained
:
__unsafe_unretained NSString *propertyName = nil;
[invocation getArgument:&propertyName atIndex:3];
很好,很好!!但我想了解原因。谁能解释一下?
该签名的-[NSInvocation getArgument:atIndex:]
IS
- (void)getArgument:(void *)buffer
atIndex:(NSInteger)index
因此buffer
是无类型的,不一定是对象。ARC的授权你使用一个非保留引用(因此未保留的部分),不保持引用的对象活着并能吊着(因此危险或不安全的),所以它必须是合格的__unsafe_unretained
,也看到了铿锵规范,你必须告诉ARC不要在意这块内存。
不再建议真正使用NSInvocation,它在ARC上不能很好地工作,并且在Swift中不可用。或者,您可以使用-[NSObject methodForSelector:]
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句