Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it necessary to nil an assigned variable?

In some code I sometimes see this:

@property (nonatomic, assign) NSObject *foo;

...

-(void)dealloc
{
   self.foo = nil;
   ...
}

Is it really necessary to nil an object when it hasn't been retained? Won't this actually cause problems?

like image 259
Meroon Avatar asked Feb 28 '23 12:02

Meroon


1 Answers

It is good practice to set any pointer you are no longer interested in to nil. I don't advocate the use of accessors like this in -dealloc because of possible accessor side effects (like posting notifications or KVOs), but that's a controversial position and Apple is not consistent in their own code. In any case, it is good practice to nil your pointers. It certainly will not cause problems, and the habit of nil-ing your pointers in -dealloc will save you significant bugs in the future.

In this specific case it may not be necessary, but what kind of problem are you suggesting?

like image 106
Rob Napier Avatar answered Mar 11 '23 14:03

Rob Napier