views:

107

answers:

1

I'm reading the source code of an open-source project and I've encountered the following category definition in an implementation file:

@interface QCView (Private)
- (void)_pause;
- (void)setClearsBackground:(BOOL)flag;
@end

At first I thought that the setClearsBackground method was being added to the QCView class definition. But when I search through this implementation file, I find no implementation of a setClearsBackground method (although this message is sent to a QCView instance in a few places within the file).

Why would someone declare a method on a framework class like QCView but then not implement that method anywhere? My only guess is that this is a way to circumvent the compiler and call a method that isn't declared in the QCView.h file. But this seems unlikely, because how would the developer know that an implementation for this method even exists?

+6  A: 

The developer probably used a tool such as class-dump in order to generate headers which list all the methods implemented by a framework. This is useful if you need to access SPI and have no other choice. Without the declaration, Objective-C makes assumptions about the method parameters and, beyond generating a warning, may generate an incorrect method invocation.

All the usual caveats about an undocumented interface breaking in a future OS revision apply. At the minimum, you should check if the object responds to that method (-[NSObject respondsToSelector]). For extra paranoia you can wrap the invocation in an exception block, in case the method remained but its behavior changed.

Nicholas Riley
What is "SPI"?
erikprice
System Programming Interface. Essentially, any (technically) externally accessible function or method that is not explicitly documented or supported.
Nicholas Riley
Awesome answer, thanks. I downloaded class-dump and ran it on QuartzComposer.framework and the above methods do appear in the output, so I think it's safe to say that this is exactly what the author was doing. Thanks again.
erikprice