objective-c - -[NSInvocation retainArguments] 是否复制 block ?

标签 objective-c objective-c-blocks retain nsinvocation

NSInvocation-retainArguments 方法在您不立即运行 NSInvocation 而稍后运行时很有用;它保留对象参数,因此它们在此期间保持有效。

众所周知, block 参数应该被复制而不是保留。我的问题是,当参数是 block 类型时,-retainArguments 是否知道复制而不是保留参数?该文档并未表明它确实如此,但这似乎是一件简单而明智的事情。

更新:在 iOS 7 中行为似乎发生了变化。我刚刚对此进行了测试,在 iOS 6.1 及之前的版本中,-retainArguments 没有复制 block 的参数类型。在 iOS 7 及更高版本中,-retainArguments 会复制 block 类型的参数。 -retainArguments 的文档已更新为说它复制 block ,但没有说明行为何时改变(这对于支持旧操作系统的人来说真的很危险)。

最佳答案

当然应该如此(尽管我自己还没有测试过)。根据documentation :

retainArguments

If the receiver hasn’t already done so, retains the target and all object arguments of the receiver and copies all of its C-string arguments and blocks.

  • (void)retainArguments

Discussion

Before this method is invoked, argumentsRetained returns NO; after, it returns YES.

For efficiency, newly created NSInvocation objects don’t retain or copy their arguments, nor do they retain their targets, copy C strings, or copy any associated blocks. You should instruct an NSInvocation object to retain its arguments if you intend to cache it, because the arguments may otherwise be released before the invocation is invoked. NSTimer objects always instruct their invocations to retain their arguments, for example, because there’s usually a delay before a timer fires.

关于objective-c - -[NSInvocation retainArguments] 是否复制 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16072720/

相关文章:

ios - 无法通过添加 UIAlertAction 来关闭 UIAlertViewController 的警报模式

ios - block 隐式保留 'self' ; - 但这是有意的行为吗?

ios - 如何等到异步调用完成,包括完成 block (AFNetworking)

Objective-C 复制并保留

iPhone 与 CorePlot 的多点触控交互

ios - 尝试在我的应用程序中使用 TPKeyboardAvoding Scroll

ios - 如何处理并发执行的大量数据(NSOperationQueue 或 Blocks)

ios - 如何正确释放没有句柄的分配对象

ios - 内存泄漏,MutableArray 中的对象没有释放?

objective-c - 如何在 Objective-C 中进行深拷贝?