Reputation: 22956
If I want to potentially re alloc/init and reuse a property of my class under ARC, how can I destroy the previous instance?
Is it legal to call self.myProperty = nil;
and then later re alloc and init the same property?
Thanks, John
Upvotes: 1
Views: 75
Reputation: 10434
You can make the property to nil always when the reference is no more needed. When allocating the object it is not necessary to keep nil.
Reason behind: when the pointer is trying to point different location for the if there are no other reference to old location that old object will be deallocated.
Upvotes: 1
Reputation: 8202
Yes
self.myProperty = nil;
self.myProperty = [PropertyClass new]; // or [[PropertyClass alloc] init]
will do what you want. If self
is the only thing holding on (retaining) the instance, nilling it out will also call dealloc
instance method for the class of myProperty
.
Upvotes: 2