Basic Definition:
Categories are wonderful feature of the Objective C language by using categories on a class we can expand the functionality of the class without touching it.
Analogy of category:
If you a smart phone (assume it as a base class) and you have some accessories of it like blue tooth earpiece (additional functionality)

Only Class Class + Category.
iPhone iPhone + ear piece
Here we are not touching iPhone Class and adding functionality to the it that you can attend call from your earpiece.
So category will look like:
Here are some of the common concepts for categories that generally confuses us.
1. Why can't we add instance variable to the categories ?
Ans: The methods that are available in any category are added to any class at RUN Time only .
2. Difference between categories and class extension ?
Ans: Class extension is basically used to expand the public interface of the class with some private methods and properties
Ref: http://just-works.blogspot.in/2013/11/subclass-category-and-extensions-in.html
Categories are wonderful feature of the Objective C language by using categories on a class we can expand the functionality of the class without touching it.
Analogy of category:
If you a smart phone (assume it as a base class) and you have some accessories of it like blue tooth earpiece (additional functionality)

Only Class Class + Category.
iPhone iPhone + ear piece
So category will look like:
@interface iPhone (earPiece) - (iPhone *)receiveCallFromBlueTooth:(BOOL)isEnabled; @end
In the above syntax iPhone is class on which earPiece category is added.
Another Example:
We need to validate every email enter by the user every time and generally we don't have anything provided form apple and this situation come across several view controller and several projects .
An easy solution for this problem what i generally use create a category on NSString.
NSString+NSString_validateEmail.h #import <Foundation/Foundation.h> @interface NSString (NSString_validateEmail) - (BOOL) isValidEmail:(NSString *)yourString; @end NSString+NSString_validateEmail.m #import "NSString+NSString_validateEmail.h" @implementation NSString (NSString_validateEmail) - (BOOL) isValidEmail:(NSString *)yourString { BOOL stricterFilter = YES; NSString *stricterFilterString = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}"; NSString *laxString = @".+@([A-Za-z0-9]+\\.)+[A-Za-z]{2}[A-Za-z]*"; NSString *emailRegex = stricterFilter ? stricterFilterString : laxString; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; return [emailTest evaluateWithObject:checkString]; } @end TO Call this function in your VC , you just need to import the class #import "NSString+NSString_validateEmail.h" and while comparing
if(! [self isValidEmail: abc@xyz.com]) { // Alert the user if validation fails. }
Here are some of the common concepts for categories that generally confuses us.
1. Why can't we add instance variable to the categories ?
Ans: The methods that are available in any category are added to any class at RUN Time only .
2. Difference between categories and class extension ?
Ans: Class extension is basically used to expand the public interface of the class with some private methods and properties
- Its scope is local to a single class
- It added on the compile time only
- Method added in the extension of a class should be implemented in the class
Ref: http://just-works.blogspot.in/2013/11/subclass-category-and-extensions-in.html
No comments:
Post a Comment