OC Language Block and protocol

Source: Internet
Author: User

First, Bolck

(a) Introduction

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.

identifier ^

(ii) Basic use

(1) define the block variable

Int (^sumblock) (int, int); With parameters, the return value type is int

void (^myblock) ();//no parameter, return value type is empty

(2) using block encapsulation code

  

(3) block access to external variables

1) External variables can be accessed within the block;

2) By default, external local variables cannot be modified inside the block

3) Add the __block keyword to the local variable, the local variable can be modified inside the block.

(4) Use typedef to define block types (much like pointers to functions)

typedef int (^myblock) (int, int);

You can then use this type to define the block variable.

Myblock A, B;

A = ^ (int a,int b) {return a-A;};

Myblock B2 = ^ (int n1,int n2) {return n1*n2;};

II. Protocol (agreement)

(a) Introduction

1.protocol: For a single use, declare a lot of methods (cannot declare member variables), cannot write implementation.

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

3. As long as the parent class adheres to a protocol, the subclass also follows.

The 4.protocol declaration method allows any class to be implemented, and Protocol is the Protocol.

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

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

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

(ii) Basic use

Create a protocol

Compliance Agreement

Implementation of the method declared in the completion protocol

Test program

1. Definition of the Agreement

@protocol Agreement 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 on compliance with agreements

@protocol Agreement name < other agreement name >

@end;

3. Keywords in the Protocol method declaration

(1) required (default) requires implementation, if not implemented warning but no error

(2) optional does not require implementation

4. Compliance with protocol restrictions when defining variables

Class name < protocol name > * variable name nsobject <. Myprotocol> *obj;

ID < protocol name > variable name ID <. Myprotocol> obj1;

Attributes declared in 5.Property can also be used to comply with protocol restrictions

@property (Nonatomic, Strong) class name < protocol name > * attribute name;

@property (nonatomic, strong) id< protocol name > attribute name;

6. Supplemental Knowledge: The protocol itself is written in the. h header file, but can also be defined anywhere. When this protocol is only used by this class, the protocol is generally written in this class, and when the protocol requires multiple classes to be implemented, it is written in a separate file.

OC Language 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.