Automatic Reference Counting in Cocoa-touch - inline best practice
Before the ARC was in place for ios development, i have been using something like this in vi开发者_如何学JAVAewDidLoad
to setup my navigation items:
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneClicked:)] autorelease];
How can i implement this correctly while ARC is enabled for my view controller while keeping it a 1 line deal?
I know about prepending the __autoreleasing, __strong, etc, but i don't see how i can create this rightBarButtonItem
without separating it into 2 lines like this:
__autoreleasing UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneClicked:)];
self.navigationItem.rightBarButtonItem = rightBarItem;
You don't need to use __autoreleasing qualifier in this situation. The rightBarButtonItem has a strong reference for the assigned object, and then the assigned object will be automatically released when the rightBarButtonItem is released (when UINavigationBar is released).
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneClicked:)];
精彩评论