简体   繁体   English

iPhone:这两个作业之间的区别

[英]Iphone: Difference between these two assignment

i have this code in my .h: 我的.h中有以下代码:

@property (nonatomic, retain) NSArray *arrayData;

What is the difference between: 之间有什么区别?

self.arrayData = [[NSArray alloc]initWithObjects:@"date",@"trip",nil];

and: 和:

arrayData = [[NSArray alloc]initWithObjects:@"date",@"trip",nil];

and what should i use and how to release the arrayData variable. 以及我应该使用什么以及如何释放arrayData变量。

Thanks 谢谢

The difference is that using self.arrayData = ... retains the array. 区别在于使用self.arrayData = ...保留数组。 You should release it using self.arrayData = nil; 您应该使用self.arrayData = nil;释放它self.arrayData = nil; .

The code you have had here doesn't work, for init alone doesn't allocate an array. 此处 拥有 的代码无法正常工作,因为单独的init不会分配数组。 You could use 你可以用

self.arrayData = [NSArray arrayWithObjects:@"date",@"trip",nil];

To allocate and initialize the array. 分配和初始化数组。

ps the arrayWithObjects returns an allocated and autoreleased object. ps arrayWithObjects返回分配并自动释放的对象。 That means that the object will vanish if you don't retain it. 这意味着如果您不保留该对象,该对象将消失。 So use self.arrayData = ... to do so. 因此,使用self.arrayData = ...即可。

The equivalent with alloc/init/autorelease would read: 与alloc / init / autorelease等效的内容为:

self.arrayData = [[[NSArray alloc] initWithObjects:....,nil] autorelease];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM