Cocoa Mystery of the Moment

So I'm throwing together an XCode template project for new ALife models--just enough to show the proper MVC design and the bare minimum of classes and methods needed for a CocoaBugs plugin--and I run into a gem of a compiler warning on this line in the drawRect method of the view:

[[NSString stringWithFormat:@"%d", simulation.generation]
 drawInRect:[self bounds]];

Here's the warning.

/Users/d/Desktop/ALife Plugin/LifeView.m:38: warning: multiple methods named '-drawInRect:' found /Developer/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSImageRep.h:43: warning: using '-(BOOL)drawInRect:(NSRect)rect' /Developer/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSStringDrawing.h:24: warning: also found '-(void)drawInRect:(NSRect)rect'

Now, that seems pretty legit at first glance. NSString, despite my memory, has no drawInRect: method; it's drawInRect:withAttributes: for unattributed strings.

The thing that boggles me is that neither of the two candidate methods have any chance of working on the object that I am plainly calling them on. The compiler, it seems, can clearly see that I am making just a plain-old NSString, not an NSImageRep or NSAttributedString.

There's the possibility that I could be injecting a drawInRect: method to the NSString class at runtime, so it makes sense that this wouldn't be a straight-up error, but these specific warnings, I admit, confuse me greatly.

The take-away message: I still have a lot to learn about Objective-C method dispatching.

Leave a comment