OC Basics-Classification (category) and Protocol (protocol)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.