【问题标题】:can't do super dealloc and need explanation about dealloc and release what is the differences between them?不能做超级dealloc,需要解释dealloc和release它们之间有什么区别?
【发布时间】: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 方法。

标签: ios dealloc


【解决方案1】:

[super dealloc]release 在使用 ARC 时是不允许的。 ARC 的目标是自动管理它,因此您无需担心。如果您更喜欢手动释放对象,那么关闭 ARC 是不错的选择。

【讨论】:

  • 如何关闭 ARC?
【解决方案2】:

如果你使用 arc,你不能释放或释放一些东西。您可以在 dealloc 中将 _imageToDisplay 设置为 nil。

【讨论】:

    猜你喜欢
    • 2010-10-18
    • 2011-03-21
    • 2011-09-20
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 2011-09-29
    • 2021-08-10
    • 1970-01-01
    相关资源
    最近更新 更多