PDA

View Full Version : Any use for undeclared @implementations?




zippyfly
Sep 28, 2010, 09:52 PM
Hi folks.

So I've come across mention that methods not declared in @interface and written up in @implementation can still be called?

Of what use is this?

It seems such methods are not actually @private to the object. I can't see this being a mistake on the part of the designers so I'm wondering why this is allowed?



Sydde
Sep 28, 2010, 10:59 PM
Most often you see this used for overrides (methods prototyped by a superclass, like -init or -mouseDown), for protocol implementation (like NSCoding or datasource, implicitly prototyped via the protocol conformance declaration) and for delegate methods. Occaisionally you might see non-prototyped methods that are only used within the implementation, which the programmer might do in order to hide the methods, but this is a generally discouraged practice.

zippyfly
Sep 28, 2010, 11:08 PM
Most often you see this used for overrides

But of course! (Smacks forehead. Mine, not yours. ;-)

gnasher729
Sep 29, 2010, 02:33 AM
But of course! (Smacks forehead. Mine, not yours. ;-)

I recommend to set up your projects so that all compiler warnings are treated as errors, then make use of unknown selector a warning. (Especially useful when you write @selector(thismethod) instead of @selector(thismethod: ) )

I tend to have something like

@interface myclass (privatebits)
...
@end

in my implementation files for internally used methods so that these methods are not even visible in the header file.