Search code examples
objective-cinstance-variables

re-assign instance variable, is it better to set nil first


I am using XCode 7.3.1.

I have a instance variable:

mProduct = [[Product alloc] initWithName:@"apple"];

later on in the same class, I need to re-assign a new object to this instance variable, I know I can just do:

mProduct = [[Product alloc] initWithName:@"Car"];

But I am wondering, is it better to first set mProduct to nil before re-assign a new instance to the variable?

mProduct = nil;
mProduct = [[Product alloc] initWithName:@"Car"];

Solution

  • No, it will make no difference at all.

    Either way ARC will correctly handle memory, deallocating memory as needed if the old object is no longer in use in your program.