Note: I am not using ARC
I have a UILabel with the following property: @property (nonatomic, retain) UILabel *someLabel; I am trying to set a custom setter. Would the following code cause a leak, since @property is actually calling retain as well?
- (void)setSomeLabel:(UILabel *)someLabel
{
if (someLabel != self.someLabel) {
[self.someLabel release];
self.someLabel = [someLabel retain];
}
// some custom code here
}
You really, really should.
Your setter is an infinite loop. The call to
self.someLabel = ...is the exact equivalent of[self setSomeLabel:...], which causes the loop.A correct manual setter looks like this:
There are other common patterns. A major question is whether “some custom code” should run if the object is reset to the same value. If not, then this pattern makes more sense: