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?
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.