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

前端之家收集整理的这篇文章主要介绍了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
  • @H_301_20@

    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.

原文链接:https://www.f2er.com/c/118668.html

猜你在找的C&C++相关文章