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/