When transitioning to ARC, I got the following compiler error: “It is not safe to remove an unused autorelease message”.
If I simply remove the autorelease message, obj will be immediately deallocated at the end of getAutoreleasedObj, which will cause a crash in printObj. So How do I handle the autoreleased object, and convert the following code to ARC?
- (MyClass *) getAutoreleasedObj {
MyClass *obj = [[MyClass alloc] init];
[obj autorelease];
return obj;
}
- (void) printObj {
NSLog(@"%@", [self getAutoreleasedObj];
}
You don’t have to autorelease it, because it is returned from a function, ARC will implicitly make it
__autoreleasingand release it for you.This code:
Compiles to this: