I’m quite new at Objective-C and i’ve a question :
I’ve been through some Apple’s sample code and found the following :
In the top of the file, I found to uses of Objective-C categories
@interface EAGLView (EAGLViewPrivate)
- (BOOL)createFramebuffer;
- (void)destroyFramebuffer;
@end
@interface EAGLView (EAGLViewSprite)
- (void)setupView;
@end
Just after that, starts the implementation of the EAGLView class.
What is the real purpose of categories here, as the 3 functions above could also be defined directly in the header file ??
Thx
As indicated by the first category’s name (“EAGLViewPrivate”) declaring these methods in the .m file is a way of simulating private methods. Objective-C doesn’t have true support for private methods, but since these aren’t declared in the .h file, the compiler will warn when code outside the .m file where they’re declared tries to call them.
This is more commonly done with class extensions (a special case of a category) these days, mostly because using a class extension results in the compiler warning if a “private” method isn’t implemented in the class’s @implementation block. Class extensions were a new feature in Objective-C 2.0, so in older code, you’d often see a category with private in the name as in the code you’ve posted. The intent is the same.