微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

原文地址:https://www.jb51.cc/c/118668.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐