开发者

Automatic Reference Counting in Cocoa-touch - inline best practice

开发者 https://www.devze.com 2023-04-13 08:31 出处:网络
Before the ARC was in place for ios development, i have been using something like this in vi开发者_如何学JAVAewDidLoad to setup my navigation items:

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:)];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号