开发者

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 = [开发者_如何学Python[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:.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜