Gerardo
Gerardo

Reputation: 5830

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

Upvotes: 0

Views: 140

Answers (1)

mvds
mvds

Reputation: 47084

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];

Upvotes: 1

Related Questions