Categories - Objective C
Sometimes, you may find that you wish to extend an existing class by adding behavior that is useful only in certain situation. In order add such extension to existing classes , Objective C provides categories and extensions.
If you need to add a method to an existing class, perhaps to add functionality to make it easier to do something in your own application, the easiest way is to use a category.
The syntax to declare a category uses the @interface keyword, just like a standard Objective C class description, but does not indicate any inheritance from subclass. Instead, it specifies the name of the categories in parentheses , like this:
Characteristics of Category
Even through any methods added by a category are available to all instances of the class and its subclasses , you will need to import the category header file in any source code file where you wish to use the additional methods, otherwise you will run into compiler warning and errors.
In our example, since we just have a single class ,we have not included any header file in such a case we should include the header files as said above.
If you need to add a method to an existing class, perhaps to add functionality to make it easier to do something in your own application, the easiest way is to use a category.
The syntax to declare a category uses the @interface keyword, just like a standard Objective C class description, but does not indicate any inheritance from subclass. Instead, it specifies the name of the categories in parentheses , like this:
@interface ClassName (categoryName)
@end
@end
Characteristics of Category
- A category can be declared for any class, even if you don't have the original implementation source code.
- Any methods that you declare in a category will be available to all instances of the original class,as well as any subclasses of the original class.
- At runtime, there's no difference between method added by a category and one that is implemented by the original class.
#import <Foundation/Foundation.h>
@interface NSString (myAddition)
+ (NSString *)getCopyRightString;
@end
@implementation NSString(myAddition)
+ (NSString *)getCopyRightString
{
return @"Copyright Android 2017";
}
@end
int main(int argc , const char * argv[])
{
NSString *copyrigt = [NSString getCopyRightString];
NSLog(@"Accessing category: %@", copyrigt);
return 0;
}
@interface NSString (myAddition)
+ (NSString *)getCopyRightString;
@end
@implementation NSString(myAddition)
+ (NSString *)getCopyRightString
{
return @"Copyright Android 2017";
}
@end
int main(int argc , const char * argv[])
{
NSString *copyrigt = [NSString getCopyRightString];
NSLog(@"Accessing category: %@", copyrigt);
return 0;
}
Even through any methods added by a category are available to all instances of the class and its subclasses , you will need to import the category header file in any source code file where you wish to use the additional methods, otherwise you will run into compiler warning and errors.
In our example, since we just have a single class ,we have not included any header file in such a case we should include the header files as said above.
No comments: