问题描述
以下代码摘自iOS 5 Developer's Cookbook,用于说明如何将字符串写入文件。它使用了__autoreleasing而没有任何解释。为什么有必要?
The following code is taken from "The iOS 5 Developer's Cookbook" used to illustrate how to write a string to a file. It makes use of __autoreleasing without any explanation. Why is it necessary?
NSError __autoreleasing error;
...
if (![myString writeToFile:path atomically:YES error:&error)
{
NSLog(.... error.localizedFailureReason ...);
return;
}
为什么不在不使用__autoreleasing的情况下在堆栈上声明错误?
Why not just declare the error on the stack without the use of __autoreleasing?
------编辑-----
------ EDIT -----
其他问题:为什么作者声明NSError而不是NSError *?
Additional question: why is the author declaring NSError and not NSError*?
推荐答案
这是自动引用计数(ARC)系统的提示。
It's a hint for the automatic reference counting (ARC) system.
错误
对象将在NSString的代码中的某处分配,因此将其声明为 __ autoreleasing
in您的代码让ARC知道存储特性是什么。也就是说,当设置错误
时,它将是一个自动释放的对象。
The error
object will be allocated somewhere in NSString's code so declaring it as __autoreleasing
in your code lets ARC know what the storage characteristics are. That is, when error
is set, it will be an autoreleased object.
这篇关于在代码段示例中使用__autoreleasing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!