【发布时间】:2013-08-07 17:43:17
【问题描述】:
那个代码是对的吗? 它不释放并释放它说“超级释放 ARC 禁止” 什么时候需要dealloc,什么时候需要release?
#import "ImageViewController.h"
@interface ImageViewController ()
@end
@implementation ImageViewController
@synthesize imageToDisplay=_imageToDisplay;
-(IBAction)click:(id)sender
{
if ([[sender title]isEqualToString:@"Dog"])
{
[_imageToDisplay setImage:[UIImage imageNamed:@"border-collie_177061-1280x1024.jpg"]];
}
else if ([[sender title]isEqualToString:@"FakeBook"])
{
[_imageToDisplay setImage:[UIImage imageNamed:@"images.jpeg"]];
}//else if
}//click
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dealloc
{
[_imageToDisplay release];
[super dealloc];
}
@end
【问题讨论】:
-
你这里确实有两个问题,其中一个已经得到了彻底的回答:如果你使用ARC,你不需要释放项目,即使在dealloc中。第二个问题是什么是释放和释放:'释放'是你给一个对象的命令(消息),告诉它从内存中清除自己。这意味着您已经完成了该对象并且不再需要它。 'dealloc' 是一种释放对象的方法。 [super dealloc] 在该方法中用于告诉父(超级)类运行自己的 dealloc 方法。