Woodstock
Woodstock

Reputation: 22956

ARC and nilling out properties

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

Answers (2)

Rajesh
Rajesh

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

Rich
Rich

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

Related Questions