The category classification file in OC is equivalent to some classes in C #, and the Protocol protocol file in OC (essentially a header file) is equivalent to an interface in C #. Here is a brief explanation of the two files in OC today.
Since the video in the Xcode version is low, resulting in the classification files and protocol files are not found, and finally Baidu learned:
The category and protocol files in Xcode 7.2 are categorized into the Objective-c file
I. Category documents:
Function: You can extend a custom class, or a system class. The following example extends the NSString class by extending the number of digits in the string to be evaluated in the class:
NSString header file for class classification
1 #import <Foundation/Foundation.h>23@interface nsstring (number) 45// calculate the number of Arabic numerals in the string 6 -(int) numbercount; 7 8 @end
NSString. m files for classification of classes
1 #import "Nsstring+number.h"2 3 @implementationnsstring (number)4 5 //@ "abc123"6- (int) Numbercount7 {8 9 //[self length];Ten One intCount =0; A - intLen = (int) Self.length; - the for(inti =0; i<len; i++) { - //Get the character (char) corresponding to the I position - Charc =[self characteratindex:i]; - + if(c>='0'&& c<='9') - { +count++; A } at } - - returncount; - } - - @end
Main file:
1 #import<Foundation/Foundation.h>2 #import "Nsstring+number.h"3 4 intMainintargcConst Char*argv[])5 {6 7 @autoreleasepool {8 9NSString *str =@"ABC 123fdsf090900sdfds68768867";Ten One intCount =[str numbercount]; A -NSLog (@"%d", count); - } the return 0; -}
Ii.. Protocol Documents
Function: Declares a series of methods
Note: Classifications and protocols can only declare methods and cannot declare member variables
Instance:
1 //declaring a series of methods2 //both classifications and protocols can only declare methods and cannot declare member variables3 @protocolMyProtocol4 5 //default is @required6- (void) test4;7 8@required//test1 must be implemented.9- (void) test1;Ten One@optional//Test2 and test3 are optional implementations. A- (void) test2; -- (void) test3; - the @end
OC Basics-Classification (category) and Protocol (protocol)