Joe*_*oey 2 release objective-c autorelease
我经常看到如下内容:
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
Run Code Online (Sandbox Code Playgroud)
特别是在局部变量的情况下,在这种情况下使用'anotherButton'然后释放.这是否与在分配时自动释放它然后确保在方法结束之前使用它完全相同?即:
UIBarButtonItem *anotherButton = [[[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)] autorelease];
self.navigationItem.rightBarButtonItem = anotherButton;
Run Code Online (Sandbox Code Playgroud)
我问,因为我看到它在大量时间内完成了第一种方式,但感觉不太容易出错(对我而言)直接自动解决它.这只是个人风格的问题,还是这两种方法之间存在差异,除了在完成任务后有条不紊地释放对象,另一种注意要求它在一开始就被释放,这样就不会意外忘记了,两者都有优点.
| 归档时间: |
|
| 查看次数: |
123 次 |
| 最近记录: |