Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding setters not being called in Objective-C

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? ;)

like image 279
Sebastian Dwornik Avatar asked Dec 13 '22 21:12

Sebastian Dwornik


1 Answers

It's incorrectly capitalized, and capitalization matters. It should be setNumberOfSides:.

like image 165
Chuck Avatar answered Jan 13 '23 23:01

Chuck