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;
}
Why not just declare the error on the stack without the use of __autoreleasing?
—— EDIT —–
Additional question: why is the author declaring NSError and not NSError*?
It’s a hint for the automatic reference counting (ARC) system.
The
errorobject will be allocated somewhere in NSString’s code so declaring it as__autoreleasingin your code lets ARC know what the storage characteristics are. That is, whenerroris set, it will be an autoreleased object.