Hi, What is the difference between Category and Extension. I believe both are used to add custom methods in existing classes. Can someone throw light on this? Examplification with code will be really appreciated.
+3
A:
A category is a way to add methods to existing classes. They usually reside in files called "Class+CategoryName.h", like "NSView+CustomAdditions.h" (and .m, of course).
A class extension is a category, except for 2 main differences:
The category has no name. It is declared like this:
@interface SomeClass ()
@end- (void) anAdditionalMethod;
The implementation of the extension must be in the main @implementation block of the file.
It's quite common to see a class extension at the top of a .m file declaring more methods on the class, that are then implemented below in the main @implementation section of the class. This is a way to declare "pseudo-private" methods (pseudo-private in that they're not really private, just not externally exposed).
Dave DeLong
2010-08-17 06:36:36
Thank you so much.
Abhinav
2010-08-17 08:26:43
In Class-Extensiosn you're able to overwrite properties from a (public) read-only property to be internally readwrite.And since LLVM 1.5 you're able to do even more: you can now declare instance variables in a class extension so they are gone from the regular interface.
Max Seelemann
2010-08-17 13:53:39