2012-04-12 43 views
0

毫无疑问,iOS中有大量的信息:内存管理。在阅读了大量内容之后,对于某些情况下的“最佳”练习,我仍不清楚。请问我能要求澄清以下两个例子......ivars的另一个内存管理查询

我有充当了的tableView和的UIBarButtonItem数据源一个NSMutableArray称为editButton双方声明如下:

@interface MyTableViewController : UITableViewController { 

    NSMutableArray *datasourceArray; 
    UIBarButtonItem *editButton; 

} 

@property (nonatomic, retain) NSMutableArray *datasourceArray; 
@property (nonatomic, retain) UIBarButtonItem *editButton; 

@end 

我已经然后合成他们和alloc'd/init'd它们如下:

@implementation 

@syntesize datasourceArray, editButton; 

-(void)viewDidLoad { 

self.datasourceArray = [self retrieveDatasourceArray]; 

self.editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(editTable)]; 
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:editButton, nil] animated:NO]; 
[editButton release]; 

} 

-(void)retrieveDatasourceArray { 

NSMutableArray *datasource = [[[NSMutableArray alloc] initWithObjects @"example1", @"example2", nil] autorelease]; 

return datasource; 

} 

-(void)dealloc { 

[datasourceArray release]; 
[editButton release]; 
[super dealloc]; 

} 

问1:的NSMutableArray

正如你所看到的,我已经将数组的实际创建分离成了一个不同的方法,因为有很多代码从核心数据中检索并进行排序(这个问题不需要),我想将它们分离出来。因此,我选择返回一个自动发布的NSMutableArray,并将其设置为头文件中定义的self.datasourceArray。这是一个明智的,无泄漏的实现方式吗?

问题2:编辑按钮

,因为我需要后来改变editButton的标题和风格,我需要访问它,因此它声明。然后,我使用viewDidLoad方法将其分配/初始化,然后将其添加到数组中(其他按钮在此未显示),然后使用此数组将这些按钮添加到navigationBar中。然后我已经释放了editButton,因为我已经分配了它并将它交给了一个数组。鉴于我的dealloc方法,这是必要的还是必要的,甚至是在正确的位置?

提前感谢

编辑:进一步的问题3:

在访问这两种高德的其他地方在我的代码(比如打电话时[datasourceArray计数]或复位“编辑的标题'按钮‘完成’,我应该用自己的符号或不

编辑:?另一个问题4:

否则我已经使用了下面的代码来初始化一个合成的NSMutableArray。鉴于下面的答案,这是更泄漏...?

[self setDatasourceArray: [[NSMutableArray arrayWithArray: [self retrieveDatasourceArray]]; 

回答

1

1点阵列

NSMutableArray *datasource = [[[NSMutableArray alloc] initWithObjects @"example1", @"example2", nil] autorelease]; 

return datasource; 

在这里你正在做它正确 ..returning的自动释放 object..which将由变量,因为你定义它被保留是类型保留(当你做了@property)。

第二点的编辑按钮

self.editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(editTable)]; 

[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:editButton, nil] animated:NO]; 

[editButton release]; 

在这里,你显然在释放对象.. 记得变量保留你defined..so编辑按钮的新变量保留了新开的酒吧按钮项目..所以释放它。是非常必要的一个time..which你dealloc..but释放在这里做也将导致overrelease..to解决这个只是删除行发布和更新你的代码是这样

self.editButton = [[[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(editTable)]autorelease]; 
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:editButton, nil] animated:NO]; 

这里可以看到,将创建的新实例将被自动释放...并且其值将被您的变量保留

+0

非常感谢 - 非常感谢。我可能会麻烦你看看我刚刚添加的问题3吗?我之所以添加它的原因是因为这个问题:http://stackoverflow.com/questions/4080523/when-should-i-use-the-self-keyword其中有人说你在使用自己的时候应该使用init。符号。有什么想法吗? – 2012-04-12 09:26:55

+0

错字...应该*不*使用单位...原谅我! – 2012-04-12 09:34:53

+0

self.iVarName应该在你定义变量时使用@property和init和autorelease只是替代方法不使用init ...如果你不使用init..that abject是autorelesased ...和当你使用init ..你应该使用autorelease也使它成为autorealease对象再次.. – Shubhank 2012-04-12 09:53:43