Gerardo
Gerardo

Reputation: 5830

Iphone Call Accessors / Properties in initializers

Why is a bad idea to call:

self.dataArray = [NSArray arrayWithObjects:@"user",nil];

in the initializers if the dataArray is set as a property?

Thanks

Upvotes: 0

Views: 58

Answers (1)

hooleyhoop
hooleyhoop

Reputation: 9198

I seem to remember.. nothing to do with properties

self.dataArray = [NSArray arrayWithObjects:@"user",nil];

is equivalent to

[self setDataArray: [NSArray arrayWithObjects:@"user",nil]];

so you are calling the accessor -setDataArray:

Now, lets say CollegueA subclasses this class as ClassB and overides -setDataArray:

CollegueA has every right to expect that for an instance of ClassB her -init has been called and that the instance will have finished initailzion before -setDataArray: is called. ie. that in her setDataArray: method self is properly initialised. This is not what will happen with your example code. Her -setDataArray: will be called from your -init method before her -init has even been run.

In a project where you are the sole developer, assuming you are not writing a framework intended to be shared, i say not a major worry. But then i would still prefer

dataArray = [[NSArray alloc] initWithObjects:@"user",nil];

Upvotes: 2

Related Questions