众所周知,应该复制块参数而不是保留.我的问题是,-retainArguments是否知道复制而不是在块类型时保留参数?文档并没有表明它确实如此,但它似乎是一件容易和明智的事情.
更新:iOS 7中的行为似乎已经发生了变化.我刚刚对此进行了测试,并且在iOS 6.1和之前,-retainArguments没有复制块类型的参数.在iOS 7及更高版本中,-retainArguments会复制块类型的参数. -retainArguments的文档已经更新,表示它复制了块,但它没有说明行为何时发生了变化(这对支持旧操作系统的人来说真的很危险).
解决方法
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.