Block and protocol

Source: Internet
Author: User

A. Block

1. Basic Concepts

What is block? Apple's recommended type, high efficiency, saves code in the run. Used to encapsulate and save code, a bit like a function, block can be executed at any time.

Bolck and Function Similarity: (1) can save the Code (2) has the return value (3) The physical parameter (4) calls the same way.

The identifier is ^

// Output N Horizontal            lines with a block void (^lineblock) (int) =  ^ (int  N)    {        for (int0; i<n; i++ {            NSLog (@ "----------------");        }    };    Lineblock (5);

2. How to define a block variable

int (^sumblock) (intintvoid (^myblock) ();

3. How to use block encapsulation code

^ (intint  b) {    return A- b;};   ^() {    NSLog (@ "----------");};   ^ {    NSLog (@ "----------");};

4.block Access outside variables

    • External variables can be accessed inside the block
    • By default, the outer local variables cannot be modified inside the block
    • Add the __block keyword to the local variable and the local variable can be modified inside the block

5. Defining block types with typedef

int (^myblock) (intint//  can later use Myblock this type to define the block variable myblock block; Myblock B1, B2;   = ^ (intint  b) {    return A- b;};   = ^ (intint  b) {    return A- b;};

Two. Protocol

1. Basic use

Used to declare a large heap of methods (cannot declare member variables) and cannot write implementations.

2. Features

1) As long as a class complies with this agreement, it has all the method declarations in the agreement.

2) subclasses obey as long as the parent class adheres to a protocol.

3) The Protocol declaration method allows any class to be implemented, and Protocol is the Protocol.

4) OC cannot inherit multiple classes (single inheritance) but can comply with multiple protocols. Inheritance (:), Compliance Agreement (< >)

5) The base protocol:<nsobject> is the base protocol and is the most fundamental and basic protocol, which declares many of the most basic methods.

6) The agreement is subject to the agreement, and one agreement complies with the other, and it can have a method statement in another agreement.

3. How to comply with the agreement

1) Class Compliance Agreement

@interface Class Name: Parent class name < protocol name 1, protocol name 2>@end

2) Protocol Compliance Agreement

@protocol Agreement name < Other protocol name 1, other protocol name 2>@end

4. Keywords for method declarations in the agreement

1) @required (default)

Requires implementation, and if not implemented, a warning is issued

2) @optional

Does not require implementation, how can there be no warning

5. When defining a variable, limit the object that this variable holds to a protocol

Name < agreement names > * variable names;

id< protocol name > variable name;

nsobject<myprotocol> *obj;id<MyProtocol> obj2;

If the corresponding protocol is not followed, the compiler warns

The attributes declared in [email protected] can also be used as a limit to comply with the agreement

@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;

7. The agreement can be defined in a separate. h file and can also be defined in a class

1) If the protocol is used only in a class, the protocol should be defined in the class

2) If this protocol is used in many classes, it should be defined in a separate file

8. Classifications can be defined in separate. h and. m files and can also be defined in the original class

1) In general, are defined in a separate file

2) defines the classification in the original class, only requires to be able to understand the grammar

Block and 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.