简体   繁体   中英

Iphone: Difference between these two assignment

i have this code in my .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.

Thanks

The difference is that using self.arrayData = ... retains the array. You should release it using self.arrayData = nil; .

The code you have had here doesn't work, for init alone doesn't allocate an array. 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. That means that the object will vanish if you don't retain it. So use self.arrayData = ... to do so.

The equivalent with alloc/init/autorelease would read:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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