NSError和__autoreleasing [英] NSError and __autoreleasing

查看:121
本文介绍了NSError和__autoreleasing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释在以下示例代码块中使用__autoreleasing的目的吗?

Can someone please explain to me the purpose of having __autoreleasing in the following sample code block?

- (void)execute:(NSError * __autoreleasing *)error {
    // do stuff, possibly assigning error if something went wrong
}

我删除了__autoreleasing,所有内容似乎仍然可以编译/运行.我开始使用ARC后的obj-c,所以我从来没有真正学习/理解过所有那些双下划线的东西.我已经阅读了 ARC过渡指南,但我不完全理解他们的NSError示例.

I removed the __autoreleasing and everything still seems to compile/run fine. I started using obj-c post ARC so I never really learned/understood all those double underscore thingamajigs. I've read the ARC transition guide, but I don't fully understand their NSError example.

推荐答案

考虑ARC如何与变量一起使用-每个参考变量都有一个模式(隐式或显式): strong 等.此模式使ARC知道如何处理对该变量的读写.例如对于 strong 变量,读取不需要任何其他操作,而写入则需要释放变量中的现有引用,然后再用新引用替换. ARC需要知道任何变量的模式才能起作用.

Consider how ARC works with variables - each reference variable has a mode (implicit or explicit): strong, weak, etc. This mode let's ARC know how to handle reads and writes to that variable; e.g. for a strong variable reading requires no additional action while writing requires releasing the existing reference in the variable before it is replaced by the new one. ARC needs to know the mode of any variable in order to function.

现在考虑本身通过 reference 传递的变量,例如对于您的execute,您将拨打以下电话:

Now consider variables that themselves are passed by reference, e.g. for your execute you'll have a call along the lines of:

NSError *myError = nil;
...
[someObject execute:&myError]; // pass the variable itself by reference, not the variables value

execute的正文将包含与以下内容相似的赋值:

and the body of execute will contain an assignment along the lines of:

- (void)execute:(NSError * __autoreleasing *)error
{
   ...
   if (error != NULL)
      *error = [NSError ...]; // assign indirectly via the reference to a variable
   ...
}

现在,对于该间接分配,ARC需要知道引用变量的模式,以便知道如何读写.这就是声明中的__autoreleasing,它告诉ARC它已经传递了对模式为 autoreleasing 的变量的引用,并且告诉ARC如何读取和写入变量的内容.多变的.删除__autoreleasing并将采用默认模式,在这种情况下,我建议明确表示肯定会很好.

Now for that indirect assignment ARC needs to know the mode of the referenced variable so it knows how to read and write. That is what the __autoreleasing is in the declaration, it tells ARC that it has been passed a reference to a variable whose mode is autoreleasing, and that tells ARC how to read and write the contents of the variable. Remove the __autoreleasing and a default mode will be assumed, and in this case I'd suggest being explicit is certainly good.

autoreleasing 模式意味着变量包含一个不属于该引用的引用,必要时读取应保留 ,而写入则只能写入.它主要用于通过引用传递的变量.

The autoreleasing mode means the variable contains a reference which is not owned, reads should retain if necessary and writes can just write. It is used mainly for variables passed by reference.

您可能会注意到,在上面的示例中,变量myError的模式为 strong (隐式),但它通过引用作为 autoreleasing 传递-编译器处理此问题通过引入一个临时的自动释放变量,将 without 保留myError中的当前引用自动复制到该变量中,然后将该临时引用作为参数传递给execute:来自动执行.调用返回后,编译器会从临时对象到myError进行常规分配,这会导致释放任何旧引用,并保留返回的旧引用.

You might notice that in the example above the variable myError has mode strong (implicitly) and yet it is passed by reference as autoreleasing - the compiler handles this automatically by introducing a temporary autoreleasing variable, copying without retaining the current reference in myError into it, and passing the temporary by reference as the argument to execute:. After the call returns the compiler does a normal assignment from the temporary to myError, which results in any old reference being released and the returned one retained.

有关更多详细信息,请参见 Apple向ARC发行说明的过渡

For more details see Apple's Transitioning to ARC Release Notes

评论关注

问:__autoreleasing是否隐式设置?

A:好吧 Apple的文档不是特定的,但 Clang文档表示,它对于间接参数是隐式的.如上所述,我建议您保持清楚,清楚是一件好事.

A: Well Apple's document is not specific, but the Clang documentation says it is implicit for indirect parameters. As above I'd recommend being explicit, clarity is a Good Thing™.

问:安置是否重要?

A:是,不是.这是C声明,是测验问题(下面的声明是什么……").限定符应该在两个星号之间,因为它是指向对象的(类型变量)自动释放指针的指针,但是Apple声明编译器在宽容"而不具体说明其宽容.安全播放,放到正确的位置.

A: Yes, and no... This is a C declaration, the stuff of quiz questions ("What does the following declare..."). The qualifier should be between the two asterisks as it is a pointer to a (variable of type) autoreleasing pointer to an object, but Apple state the compiler is "forgiving" without being specific of what it forgives. Play it safe, put it in the right place.

问:在进行间接分配之前,是否不应该测试error是否为NULL?

Q: Should you not test for error being NULL before doing the indirect assignment?

A:您当然应该在进行间接操作之前的某个位置.所显示的代码只是概述,而...则省略了这些细节并覆盖了这些细节.但是,由于几年来它已经提出了几次,也许我对它的遗忘太多了,所以添加了一个合适的if.

A: Of course you should, somewhere before you do the indirection. The code shown is just an outline and such detail was elided and covered by the ...’s. However as it has been raised a few times over the years maybe I elided too much, a suitable if has been added.

这篇关于NSError和__autoreleasing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆