Category
The category appears primarily to add methods to classes that do not have source code (only declarations). For example, the class provided by OC is only the. h declaration section, without the. m implementation section. At this point we can add to a class the functionality we want to implement by adding the code method that will be added to that class as part of that class, and if the class is inherited, the category code will be inherited.
Notice the points:
1, the category can not add the instance variable, and extension is different.
2, the category can add methods.
3. If the method we define in category has the same name as a method in the class, it will be overridden, and only the method in the category will be executed, and the same method within the class will not be executed. So when we write category, we should avoid this method of duplicate name, which requires us to precede the method with some recognition prefix, generally with the first letter and underscore prefix.
Create category
Command+n, in the panel, select OS X--->sourse->objective-c file,next,file type select Category,class choose which class we want to add category to, File writes our function name.
The file name of the newly created category is the class name + function name. h and. m pair of files.
As with normal classes, the method is declared in the. h file (Note that you cannot add an instance variable) and is implemented in the. m write method. Note that the method should be prefixed.
Nsstring+sayhi.h
#impor t <foundation/foundation.h> , @interface NSString (sayhi)// Category, not a class, is a class of nsstring //category cannot add an instance variable //category can add method //the priority of the method in the class, higher than this class, if the same name, if the same name, first call the method in the class. //so, write class intent method must prefix + (void) dh_saywhat; + (void) saywhat; &NBSP; -(void) dh_sayhi; -(void) sayhi; //rewrite convenience builder + (instancetype) dh_string; + (instancetype) string; //overriding instance method -(NSString *) Dh_substringfromindex: (Nsuinteger) from; -(NSString *) Substringfromindex: (Nsuinteger) from; @end |
NSSTRING+SAYHI.M file
#impor T "nsstring+sayhi.h" &NBSP; @implementation NSString (sayhi) -(void) sayhi{ NSLog (@ "Want to stay can not stay the most lonely."); + (void) saywhat{ NSLog (@ "Hello what"); //rewrite convenience builder + (instancetype) string{ NSLog (@ "Override Convenience Builder:"); return nil; //Override instance method -(NSString *) Substringfromindex: (Nsuinteger) from{ NSLog (@ "instance: Is it really me? "); return nil; , &NBSP; @end |
Main.m
#import <Foundation/Foundation.h> #import "Nsstring+sayhi.h" int main (int argc, const char * argv[]) { @autoreleasepool { April 21, 2015 11:23:17 Beijing NSString *str = [NSString string]; [Str sayhi];//instance method [NSString saywhat];//class method [NSString string];//2015-04-21 11:42:38.945 oclesson7_category[1402:60572] Rewrite convenience builder: NSString *str = [[NSString alloc]init]; [Str substringfromindex:1];//2015-04-21 11:46:14.511 oclesson7_category[1423:61799] instance: Is it really me? } return 0; } |
In the above example, some of the methods that rewrite the NSString class are executed in main, and the methods defined in our category are performed. Therefore, it is emphasized that the writing category method must be prefixed.
An example:
Write class methods to convert strings into nsdate
Nsdate+stringtodate.h
#import <Foundation/Foundation.h> @interface NSDate (stringtodate) + (nsdate*) ls_datewithdatestring: (NSString *) astring; @end |
Nsdate+stringtodate.m
#import "Nsdate+stringtodate.h" @implementation NSDate (stringtodate) + (nsdate*) ls_datewithdatestring: (NSString *) astring{ NSDateFormatter *nsf = [[NSDateFormatter alloc]init]; [NSF setdateformat:@ "YYYYMMDDHHMMSS"]; NSDate *date = [NSF datefromstring:astring]; Nstimezone *zone = [[Nstimezone alloc]init]; Nsinteger offset = [zone SECONDSFROMGMT]; return [date Datebyaddingtimeinterval:offset]; } @end |
Main.m
#import <Foundation/Foundation.h> #import "Nsdate+stringtodate.h" int main (int argc, const char * argv[]) { NSDate *date = [nsdate ls_datewithdatestring:@ "20140402142850"]; NSLog (@ "%@", date); } return 0; } |
Category (19)