Black Horse programmer ___ OC _ protocol and category

Source: Internet
Author: User

Protocol and category

Category)

When you want to add some methods on the basis of a class, but you cannot change the content of the original class, you can use classification.

The format for creating a category is: original class name + category alias. H (. m)

Note:

1. Only methods can be added for classification, but member variables cannot be added.

2. You can access the member variables declared in the original class in the classification method implementation.

3. Classification can re-implement the methods in the original class, but will overwrite the original method, which will make the original method unusable.

4. When both the classification of a method and the original implementation are implemented, the call is sequential.

Method call priority: Classification (the last type involved in compilation takes precedence)> parent class

Protocol)

Usage

Sometimes, when we write a class method, if we want to use another class method, we will avoid having to worry about it in the future, and we will be able to make that class comply with a protocol, when the classes we write comply with the Protocol, we can use the method declaration we want. The protocol is like a shared platform for method declaration.

1. Protocol definition

@ Protocol name <nsobject>

// Method declaration list ....

@ End

2. How to comply with the agreement

1> class Compliance Agreement

@ Interface Class Name: parent class name <protocol name 1, protocol name 2>

@ End

2> agreement compliance

@ Protocol name <other protocol name 1, other protocol name 2>

@ End

3. Keyword of method declaration in the Protocol

1> @ required (default)

Implementation required. If not, a warning will be issued.

2> @ optional

Implementation is not required. How can we avoid warnings?

4. When defining a variable, restrict the objects stored in the variable to comply with a certain protocol.

Class Name <protocol name> * variable name;

ID <protocol name> variable name;

Nsobject <myprotocol> * OBJ;

ID <myprotocol> obj2;

If the protocol is not followed, the compiler will warn

The attribute declared in [email protected] can also be used as a protocol-compliant restriction.

@ Property (nonatomic, strong) class name <protocol name> * attribute name;

@ Property (nonatomic, strong) ID <protocol name> attribute name;

@ Property (nonatomic, strong) Dog <myprotocol> * dog;

@ Property (nonatomic, strong) ID <myprotocol> dog2;

6. the Protocol can be defined in a separate. h file or a class.

1> if this Protocol is only used in a class, the protocol should be defined in this class.

2> if this protocol is used in many classes, it should be defined in a separate file.

7. the category can be defined in a separate. h and. M files, or in the original class.

1> generally, they are defined in separate files.

2> category defined in the original Class

Protocol application: proxy Mode

As mentioned above, the platform that uses the Protocol method to declare can design some proxy modes, so that we have a clear division of labor in our actual operations to facilitate team collaboration, the proxy mode also reduces the coupling between the original classes and proxies.

Black Horse programmer ___ OC _ protocol and category

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.