Override @property setter and infinite loop Override @property setter and infinite loop ios ios

Override @property setter and infinite loop


Just assign to the instance variable directly, without using dot syntax to call the setter:

- (void) setProp1:(id)aProp{    self->prop1 = aProp;}

That kind of begs the question though. All this accessor does is exactly what the parent would have done - so what's the point of overriding the parent at all?


With XCode 4.5+ and LLVM 4.1 there is no need to @synthesize, you will get a _prop1 to refer to.

- (void) setProp1:(id)aProp{    _prop1 = aProp;}

Will work just fine.


You shouldn't use "self" inside the setter since that creates the recursive call.

Also, you should check to make sure you're not assigning the same object, retain the new object and release the old object before assignment.

And you should redefine the setter name, as suggested above:

@synthesize prop1 = prop1_; ...- (void) setProp1:(id)aProp{    if (prop1_ != aProp) {        [aProp retain];         [prop1_ release];         prop1_ = aProp;    }}