【发布时间】:2011-08-17 23:56:03
【问题描述】:
我经常看到类似下面的东西:
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
特别是在使用本地变量的地方,在这种情况下是“另一个按钮”,然后再发布。这与分配它时自动释放它并确保在方法结束之前使用它完全一样吗?即:
UIBarButtonItem *anotherButton = [[[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)] autorelease];
self.navigationItem.rightBarButtonItem = anotherButton;
我问是因为我看到它在很多时候都是以第一种方式完成的,但是(对我来说)感觉不太容易出错(对我来说)直接自动释放它。这只是个人风格的问题,还是这两种方法有区别,除了一种在完成后有条不紊地释放对象,另一种注意一开始就声明它被释放,以免意外算了,各有千秋。
【问题讨论】:
标签: objective-c release autorelease