After poking around some header files in the Foundation
framework (NSString.h
for example), I noticed that the declaration for NSString
looks like:
@interface NSString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding>
/* NSString primitive (funnel) methods. A minimal subclass of NSString just needs to implement these, although we also recommend getCharacters:range:. See below for the other methods. */
- (NSUInteger)length;
- (unichar)characterAtIndex:(NSUInteger)index;
@end
The rest of the methods are declared in categories, which in turn are declared in the same header file.
After reading this answer, the implication seems to be mostly for organizing code.
Is there a benefit (other than logical grouping of methods) to declaring categories in the same header as the class declaration, especially in the case where the implementation is hidden, as in the case of NSString
?
Generally, yes, categories are used for organisational purposes. But in the case of NSString
they are mainly used because NSString
is a class cluster. As such, the categories are used to define the additional methods (as opposed to the primitive methods) of the classes in the cluster.
There's a nice write up about class clusters from Mike Ash here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With