ios - 为什么缓冲区需要在 NSInvocation 上被 __unsafe_unretained - getArgument :atIndex:?

标签 ios objective-c

我正在使用 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/

相关文章:

objective-c - 将xcode项目编译成库文件

ios - 仅针对 UITableViewCell 的一部分调用 didSelectRowAtIndexPath

ios - Swift 语言的 UITapGestureRecognizer

ios - 供应配置文件与包标识符不匹配

objective-c - 不兼容的指针类型

c++ - 是否可以直接在浏览器中运行 C 代码?

ios - 如何找出SQLITE查询结果为NULL?

ios - 旋转背景渐变xcode

ios - Swift UIView sizeToFit() 总是返回 0 的高度

ios - 有没有办法呈现已经加载的 View Controller ?