category (category, Extension)(i) Division of Classifications(2) 1, (named category) categoryCategory: Only new methods can be added and new variables cannot be added. 2. Extensions of (unnamed category) classesExtension://extensions are generally written directly in the implementation file of the class
// extensions are defined as instance variables and methods that are private in the class
(ii) Use of classifications
(1) Classification can only add methods (including class methods and Object methods), cannot increase member variables
(2) The member variables in the original class can be accessed in the implementation of the classification method;
(3) Classification can be re-implemented in the original class method, but will overwrite the original method, resulting in the original method can no longer use (warning);
(4) Priority of method invocation: Classification---The original class--parent class, if it contains more than one classification, then the final participation in the compilation of the classification takes precedence;
(5) In many cases, it is often possible to add categories to the system's own classes, such as NSObject and nsstring, because sometimes the system classes may not meet our requirements.
(6) In large-scale applications, the corresponding function is usually written in a classification, can have unlimited classification, the original class to expand, General sub-module write, a module a classification.
The custom string class
1 //String.h2 //category3 //4 //Created by Ma C on 15/8/12.5 //Copyright (c) 2015 BJSXT. All rights reserved.6 //7 8 #import<Foundation/Foundation.h>9 Ten @interfaceString:nsobject One@property (Nonatomic,strong) nsstring*string; A-(ID) Initwithstring: (nsstring*) str; --(void) print; - @end
1 //STRING.M2 //category3 //4 //Created by Ma C on 15/8/12.5 //Copyright (c) 2015 BJSXT. All rights reserved.6 //7 8 #import "String.h"9 //#import "String_string_private.h"//import a class extension file separately in the string classTen One //class extension extension, the added properties str1 and the Show method are private and can only be accessed in the String class A @interfaceString ()//writes the class extension file directly to the implementation of the String class - { -NSString *str1; the } --(void) show; - @end - + - @implementationString +-(ID) Initwithstring: (nsstring*) Str A { atSelf =[Super init]; - if(self) - { -_string =str; - } - returnSelf ; in } --(void) Show to { +NSLog (@"string is runing"); - } the-(void) Print//accessing the properties and methods added to the class extension through the Print method * { $ [self show];Panax NotoginsengNSLog (@"%@", _string); - } the @end
Separate class extension files
1 //string_string_private.h2 //category3 //4 //Created by Ma C on 15/8/12.5 //Copyright (c) 2015 BJSXT. All rights reserved.6 //7 8 #import "String.h"9 Ten @interfaceString () One { ANSString *str1; - } - //@property (Nonatomic,strong) nsstring* str1; the-(void) show; - @end
Category file
1 //string+count.h2 //category3 //4 //Created by Ma C on 15/8/12.5 //Copyright (c) 2015 BJSXT. All rights reserved.6 //7 8 #import "String.h"9 //The category Count of String, the method that is added, extends the functionality of the String class (remember: Only methods can be added to the category)Ten @interfaceString (Count) One-(Nsinteger) nmuberofstring: (NSString *) str; A-(Nsinteger) Numberofcount: (NSString *) str; - @end
1 //STRING+COUNT.M2 //category3 //4 //Created by Ma C on 15/8/12.5 //Copyright (c) 2015 BJSXT. All rights reserved.6 //7 8 #import "string+count.h"9 Ten @implementationString (Count) One-(Nsinteger) nmuberofstring: (NSString *) Str A { -Nsinteger count =0; - for(intI=0; i<str.length;i++) the { -Unichar ch =[str characteratindex:i]; - if(ch>='0'&& ch<='9') -count++; + } - returncount; + } A-(Nsinteger) Numberofcount: (NSString *) Str at { -Nsinteger count =0; - for(intI=0; i<str.length;i++) - { -Unichar ch =[str characteratindex:i]; - if(ch>='0'&& ch<='9') incount++; - } to returncount; + } - @end
Main function test
1 //main.m2 //category3 //4 //Created by Ma C on 15/8/12.5 //Copyright (c) 2015 BJSXT. All rights reserved.6 //7 8 #import<Foundation/Foundation.h>9 #import "string+count.h"Ten #import "String.h" One intMainintargcConst Char*argv[]) A { - @autoreleasepool - { the //Test class Extension (unnamed category) keyword: extension -String *string= [[String alloc]initwithstring:@"Xiayuan"]; -[stringprint]; - + - //test category (named category) Keywords: category +Nsinteger a = [stringNmuberofstring:@"Xu3984jjsn6ew32"]; ANSLog (@"A =%ld", a); at -Nsinteger B = [stringNumberofcount:@"36y3jfse93u4gdf8"]; -NSLog (@"B =%ld", b); - } - return 0; -}
Run results
2015-08-12 15:24:06.281 category [1304:75695] string is runing
2015-08-12 15:24:06.282 category [1304:75695] Xiayuan
2015-08-12 15:24:06.282 category [1304:75695] A = 7
2015-08-12 15:24:06.282 category [1304:75695] b = 7
Program ended with exit code:0
Objective-c: Categories (category, extension)