Does nil need to assign a variable?

In some code, I sometimes see this:

@property (nonatomic, assign) NSObject *foo; ... -(void)dealloc { self.foo = nil; ... } 

Is it really necessary that the zero of an object when it has not been saved? Wouldn't that cause a problem?

+4
source share
3 answers

It’s good practice to set any pointer that you are no longer interested in nil. I am not a -dealloc of using such accessories in -dealloc because of possible access side effects (such as wiring notifications or KVOs), but that the controversial position and Apple are incompatible in their own code. In any case, it is good practice to zero your pointers. This will certainly not cause problems, and the habit of nil-ing your pointers in -dealloc save you significant errors in the future.

In this particular case, this may not be necessary, but what problem do you propose?

+3
source

The assignment property does not need its ivar set to be zero in -dealloc.

Installing ivar on nil in -dealloc is harmless. Using an open accessor to set a property to nil in dealloc (either [self setFoo: nil] or self.foo = nil) can have unwanted side effects.

+3
source

Assuming this is a synthesized accessor, there is no point in doing this in dealloc. Previously, you could refuse if you do not want to talk to the object anymore. I do not see how this will cause problems.

If this is not a synthesized accessory, the setter may somehow affect the feedback, in which case you may need to set it to nil to tell foo that you won't be around anymore.

+2
source

All Articles