我正在使用 NSInvocation
并需要从中检索其中一个属性。
我正在使用以下代码,但我在调用 [invocation invoke];
时有一些奇怪的行为:
NSString *propertyName = nil;
[invocation getArgument:&propertyName atIndex:3];
我读到为了让它在 ARC 下工作,我们需要使用 __unsafe_unretained
:
__unsafe_unretained NSString *propertyName = nil;
[invocation getArgument:&propertyName atIndex:3];
成功了,很好!!但我想了解为什么。谁能解释一下?
最佳答案
signature对于 -[NSInvocation getArgument:atIndex:]
是
- (void)getArgument:(void *)buffer
atIndex:(NSInteger)index
所以 buffer
是无类型的,不一定是对象。 ARC 要求您使用非保留引用(因此是 unretained 部分),它不会使引用的对象保持事件状态并且可以悬空(因此是危险的或不安全的),因此它必须是 qualified作为 __unsafe_unretained
,另见 clang specification ,你要告诉ARC不要关心这 block 内存。
不再真正推荐使用 NSInvocation,它不能很好地与 ARC 一起工作并且在 Swift 中不可用。作为替代方案,您可以使用 -[NSObject methodForSelector:]
.
关于ios - 为什么缓冲区需要在 NSInvocation 上被 __unsafe_unretained - getArgument :atIndex:?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33422947/