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

当你不立即运行NSInvocation但后来执行时,NSInvocation的-retainArguments方法很有用.它保留了对象参数,因此它们在此期间保持有效.

众所周知,应该复制块参数而不是保留.我的问题是,-retainArguments是否知道复制而不是在块类型时保留参数?文档并没有表明它确实如此,但它似乎是一件容易和明智的事情.

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

解决方法

它当然应该(尽管我自己没有测试过).根据 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.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...