views:

300

answers:

1

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

+4  A: 

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

Chuck
+1 method names are case-sensitive.
Dave DeLong
So even though my variable is 'numberOfSides', (small 'n' for "number"), the synthesize feature *changes* it to a capital 'N' for its own purpose. nice. :/
Sebastian Dwornik
No, the @synthesize changes the case to be exactly in line with the camel casing rule that is consistent across all of Cocoa. 'setnumberOfSides:' has never made sense.
bbum