Objective C Categories by Example
When we need to add methods to existing class, then we can use categories. A simple category for NSString is shown below.
// Category Name - EezyAdditions
@interface NSString(EezyAdditions)
// eezy method
- (void)sampleMethod;
@end
// Category Name - EezyAdditions
@implementation NSString(EezyAdditions)
// eezy method
- (void)sampleMethod{
NSLog(@"extended method");
}
@end
int main(int argc, const char * argv[])
{
// Autoreleasepool used in main for automatic memory management.
@autoreleasepool {
NSString *string= [[NSString alloc] init];
[string sampleMethod];
}
return 0;
}