本文介绍了iOS内存管理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Xcode的功能并将其从nib文件拖到.h和.m文件时,Xcode将代码添加到deallocviewDidUnload中.它添加了我通常不添加的额外代码.我只是好奇是否需要这些额外的代码.

When you use Xcode's feature and drag from the nib file into the .h and .m file, Xcode adds the code in the dealloc and viewDidUnload. It adds extra code I do not normally add. I am just curious if this extra code is needed.

我会完成[self setDisplaySlider:nil]而不是disp = nil[disp release].

这是必要的吗?我认为您不必发布disp.

Is this necessary? I don't think you have to release disp.

@interface ViewController : UIViewController
{
   IBOutlet UISegmentedControl *disp;
}

@property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider;

@end

- (void)viewDidUnload
{
    [self setDisplaySlider:nil];
    [disp release];
    disp = nil;
    [super viewDidUnload];
}

- (void)dealloc {
    [displaySlider release];
    [disp release];
    [super dealloc];
}

推荐答案

在我看来,您为类提供了额外的代码.我会尽力解释.

In my opinion you provided a class with extra code. I'll try to explain.

首先,在上一个版本中,您有两个不同的IBOutlet.我认为您已经添加了第一个.

First of all, in your previous you have two different IBOutlet. I think that the first one has been added by you.

IBOutlet UISegmentedControl *disp;

相反,第二个代码是在您进行拖放操作时由Xcode添加的.

The second one, instead, has been added by Xcode when you made the drag and drop oeration.

@property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider;

首要考虑

术语IBOutlet只是Xcode的占位符.借助它,Xcode可帮助您将实例变量连接到图形元素.

The term IBOutlet is only a placeholder for Xcode. By means of it, Xcode helps you to connect an instance variable to a graphical element.

当我使用插座连接时,我通常会提供像@property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider;这样的访问器(就像Xcode一样),因为如果不这样做,可能会导致内存泄漏问题.

When I use outlet connections I usually supplying an accessor like @property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider; (as Xcode did) because if not you could incurr in memory leak problems.

第二考虑

Xcode提供的代码是正确的.进行拖动操作时,将displayController实例变量与图形元素链接在一起.为了平衡这种连接,必须在dealloc方法中释放该实例变量,如下所示:

The code Xcode has provided is right. When you made the drag operation you linked toghether displayController instance variable with your graphical element. To balance this connection, that instance variable has to be released in dealloc method like the following:

[displayController release];

成瘾时,Xcode添加了[self setDisplaySlider:nil];,因为在内存警告情况下可以调用viewDidUnload方法.这里没有问题,因为当控制器再次加载到内存中时,插座连接将恢复.

In addiction, Xcode added [self setDisplaySlider:nil]; because in memory warning situations viewDidUnload method could be called. No problem here because whene the controller is loaded in memory again, the outlet connection is restored.

可以在高级内存管理文档 release-or-设置为零保留成员.请注意,如果您这样做:

The difference between the two method calls can be read in Advanced Memory Management doc and release-or-set-to-nil-retained-members. Note that if you do this:

[displayController release];

您可以直接访问名为displayController的实例变量,而如果要执行此操作:

you access directly your instance variable called displayController, while if you do this:

[self setDisplaySlider:nil]; // or self.displaySlider = nil;

您访问该实例变量的访问器(在这种情况下为setter方法).这是不一样的(为避免混淆,请参阅我提供的代码).

you access the accessor (the setter method in this case) for that instance variable. It's not the same (to avoid confusion see the code I provided).

因此,这是我要使用的代码(我添加了一些注释来指导您):

So, this is the code I would use (I added some comments to guide you):

//.h
@interface ViewController : UIViewController
{
    UISegmentedControl *disp; // instance variable called disp
    // (A) now this is no longer necessary, new compile mechanism will create an instance
    // variable called _displaySlider under the hood
}

@property (retain, nonatomic) IBOutlet UISegmentedControl *displaySlider;

@end

//.m
@synthesize displaySlider = disp;  // I say to Xcode to create a setter and a getter to access the instance variable called disp as written in @property directive
// no longer necessary for (A)

- (void)viewDidUnload
{
    [super viewDidUnload];
    [self setDisplaySlider:nil]; // I call the setter method to release the UISegmentedControl
}

- (void)dealloc {
    [disp release]; // I release the UISegmentedControl directly
    // if you choose the (A) mechanism simply do
    // [_displaySlider release];
    [super dealloc];
}

希望有帮助.

这篇关于iOS内存管理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:25