I'm debugging a sample tutorial snippet and am confused about the overriding of setters.
I declare and override as shown here:
//
// PolygonShape.h
//
@interface PolygonShape : NSObject
{
int numberOfSides;
}
@property int numberOfSides;
//
// PolygonShape.m
//
@synthesize numberOfSides;
// custom setter.
- (void) setnumberOfSides:(int) i
{
if ((i > minimumNumberOfSides) && (i <= maximumNumberOfSides))
numberOfSides = i;
else
NSLog (@"Number of sides outside limits:\n You entered %d, limits are min.:%d, max.:%d",
i, minimumNumberOfSides+1, maximumNumberOfSides);
}
//
// main.m
//
poly = [[PolygonShape alloc] init];
poly.numberOfSides = 2;
[poly setnumberOfSides:2];
So my assumed thought here is that, since I "override" the synthesized setter for numberOfSides
, then poly.numberOfSides = 2;
would have called my (void) setnumberOfSides:(int) i
function. But instead, the only way that function gets called is when I explicitly do [poly setnumberOfSides:2];
I don't get it. What's the point of overriding then?
Or more likely, what am I doing wrong? ;)
It's incorrectly capitalized, and capitalization matters. It should be setNumberOfSides:
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With