Protocol and category in objective-C)

Source: Internet
Author: User

1. What is a protocol?

2. Description and use of protocols and categories

 

1. What is a protocol?

In objective-C, multi-inheritance is not supported, that is, a class cannot have multiple parent classes, but OC provides similar implementation methods, that is, protocols. The Protocol is a bit similar to the interface in Java. The difference is that the protocol can provide optional methods and does not require full inheritance.

Formal and informal protocols are included.

Formal Protocol: it declares a list of methods. The implementer of the Protocol needs to implement the declared method. You can use the @ required and @ Optional keywords to specify whether the method must be implemented or optional. The default value is required. The subclass inherits the protocol used by its parent class. A protocol can also use another protocol.

Informal protocol (category): A type is a language feature of objective-C. It allows you to add methods to a class without subclass. All nsobject or its subclass types are informal protocols. A class does not need to implement every method in informal protocols, but only needs to implement the method it wants. Classes that declare informal protocols can only be used before sending a protocol message to a target object, they must first send the respondstoselector: Message to it and get a positive answer.

2. Description and use of protocols and categories

The formal agreement Declaration is very simple, above the class declaration

@ Protocol protocolname

// Method declaration

@ End

Declare in another class that accepts the Protocol

@ Interface classname: nsobject <nscopying>

{

// Instance variable

}

// Method

@ End

You can also use multiple protocols separated by commas (,).

@ Interface classname: nsobject <nscopying, nscoding>

{

// Instance variable

}

// Method

@ End

Optional and required protocols

@ Protocol protocolname
-(Void) requiredmethod;
@ Optional
-(Void) anoptionalmethod;
-(Void) anotheroptionalmethod;
@ Required
-(Void) anotherrequiredmethod;
@ End // myprotocol

 

Informal protocol (Category) Statements and usage, category names must also comply with the camel naming method. The class description and implementation can be placed in a separate "class name. h/class name. m", or in the. h/. M file of an existing class.

@ Interface classname (categoryname)

// Method definition

@ End

Implementation

@ Implementation classname (categoryname)
// Method implementation
@ End

You must note that you can only add methods to a category and cannot add instance variables.

Bytes ------------------------------------------------------------------------------------------------------------------------

When the class name is not declared, it is an anonymous class or class extension. You can add instance variables and methods for existing classes. It can only be placed in. h/. m of an existing class.

@ Interface classname (){

// Instance variable -- can only be used in its own class

}

// Method definition-the method declared in the. h file is public, and the method declared in. m is private.

@ End

Related Article

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.