objective-c properties - getters and setters
getter-setter, ios, objective-c, uiview
Solution
You should be able to access the ivar directly as `_scale`. Your getter/setter would then look like:
Update: As @wattson12 points out in the comments below you will need to add an `@synthesize` to your implementation.
@synthesize scale = _scale;
-(CGFloat)scale
{
if (!_scale) {
return DEFAULT_SCALE;
}else{
return _scale;
}
}
-(void)setScale:(CGFloat)scale
{
if (scale != _scale) {
_scale = scale;
[self setNeedsDisplay];
}
}
Problem
In a UIView subclass I have this property: ``` @property (nonatomic) CGFloat scale; #define DEFAULT_SCALE 0.90 ``` and this getter and setter: ``` -(CGFloat)scale { if (!self.scale) { return DEFAULT_SCALE; }else{ return self.scale; } } -(void)setScale:(CGFloat)scale { if (scale != self.scale) { self.scale = scale; [self setNeedsDisplay]; } } ``` This isn't correct, because for example checking self.scale in the getter causes an infinite loop. What is the proper way to write the getter and setter so that I don't get infinite looping?