I'm desperately trying to understand the usage of __autoreleasing
keyword in Objective-C
. I have thoroughly read answers to the following questions:
In which situations do we need to write the __autoreleasing ownership qualifier under ARC?
Use of __autoreleasing in code snippet example
and despite now i understand more i still can't get the main thing, the purpose. Why do that? Let me explain what exactly confuses me. Consider the code:
@interface MyError : NSError
@end
@implementation MyError
- (void)dealloc
{
NSLog(@"My error dealloc");
}
@end
@interface ErrorHandler : NSObject
- (void)handleError:(MyError* __strong*)error;
@end
@implementation ErrorHandler
- (void)handleError:(MyError* __strong*)error
{
*error = [[MyError alloc] initWithDomain:@"Blabla" code:100500 userInfo:@{
NSLocalizedDescriptionKey : @"TestDescription"
}];
}
@end
- (void)test
{
MyError *error = nil;
ErrorHandler *handler = [ErrorHandler new];
[handler handleError:&error];
NSLog(@"Localized description %@", error.localizedDescription);
}
I wrote this code to see what happens if i don't use __autoreleasing
. As you see handleError
method accepts a reference to a reference which is explicitly declared as __strong
. And nothing happens. Everything is ok. I'm able to get the info from the MyError
object and it has been successfully deallocated, i see that. If i put __autoreleasing
instead of __strong
nothing changes. So why use __autoreleasing
if it changes nothing? This is what i don't understand. Can anyone show me what i'm missing? Thanks everyone