iOS protocol, proxy, block and callback, category

Source: Internet
Author: User

"Agreement" (Protocol)

A protocol is a mechanism for standardizing interfaces and for interacting between objects. Similar to the interface in Java, you can simulate.

Permissions for the agreement

@optional//Optional

@required//Required

Agent

I. The concept of an agent

The "note" agent is a mechanism for implementing inter-object communication between two classes.

The "principal" active party holds an ID pointer with the agreement and can use the agreement.

The "Agent" passive party complies with the Protocol and implements the method.

Proxy callback (reverse pass-through value)

When we need to complete a functional module, but do not know which object to use the function module, through the protocol, you can specify which method of receiving the data to obtain the data. The process of calling the method is called a "callback"

"Block"

"Note" Block is the object of the code block in the hypervisor

Blocks are similar to function pointers, and can be recorded as changing the * of a function pointer to ^

Int (^block) (int, int);

"Note" Block is a pointer to a block object

Block is a block object pointer

block = ^ (int a, int b) {

NSLog (@ "This block of code is used");

return a + B;

};

This is not a function, it's a code block object

NSLog (@ "%d", Block (4, 8));

"Note" The __block keyword, declaring a variable so that the variable can be used both inside and outside the block.

Category

"Problem" Sometimes we use a class (official, third party, created by ourselves) will sigh, if this class has another what kind of method is better used, more suitable for the current program. But we are not easy to modify the original class.

"A" You can use categories to add methods to classes

I. Recognition categories (category)

The "note" category is an upgrade patch for the class, and you can add member methods to the class. In addition, classes can be classified as a method of class management.

Note

1. You cannot add a member variable in a category

2. The message added to the category is as if it were added to the original class, and the object's method can use the member variable of the original class object.

3. Use categories to import category header files.

4. Methods in the parent class category, subclasses are also available.

Note

1. If there are few methods in the category and the category is used only for the current file, you can declare the category directly to the current file.

2. If the implementation of the category and the invocation of a method in the category are in the same file, the interface portion of the category can be omitted

Two. Anonymous category

"Note" category of "bad career" of distant relatives.

"See CategoryDemo2"

Three. Predecessor statement

Note: When you write a program, do not include the header file of another class in the header file of one class, so that you do not have a "cross-contained" file with each other.

Note: Class A uses class B and should include the class B header file in the implementation section.

Note If the Class A interface section uses the class B name, you should do a pre-declaration of Class B

@class B;

@class class Name

iOS protocol, proxy, block and callback, 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.