Dark Horse programmer-block code block and Protocol

Source: Internet
Author: User

I. Block

Block is a variable. This variable is similar to the function pointer in C language and is used in many callback scenarios. It can encapsulate the code and has a return value with a form parameter. The following code describes the definition and use of a block.

First, we can see that there is no returned value and no block with the form parameter.

1  void (^myblock)() = ^{2         NSLog(@"");4     };

Block with return values and parameters

1  int (^sumblock)(int, int) = ^(int a, int b){2         return a + b;3     };4     5     int c = sumblock(10, 11);

Use typedef to define the block type, which is similar to the function pointer.

 1 typedef int (^MyBlock)(int, int); 2 myBlock sumb = ^(int a,int b){ 3         return a + b; 4     }; 5     myBlock subb = ^(int a,int b){ 6         return a - b; 7     }; 8     myBlock multiply = ^(int a,int b){ 9         return a * b;10     };11     12     NSLog(@"\n6 + 4 = %d\n6 - 4 = %d\n6 * 4 = %d",sumb(6,4),subb(6,4),multiply(6,4));

Note:

The block can access external variables, but by default, the external local variables cannot be modified unless the _ block keyword is added. In addition, the block is mainly used in callback scenarios and can replace some delegate functions.

Ii. Agreement

The Protocol is a list of method signatures in which several methods can be declared. Classes that comply with this protocol implement several methods defined in this Protocol.

The Protocol is mainly used to implement proxy, master the following proxy design code, and basically master the protocol.

Declare an agreement

1 # import <Foundation/Foundation. h> 2 3 @ protocol ticketdelegate <nsobject> 4 5 // The returned fare is 6-(double) ticketprice; 7 8 // how many tickets are left 9-(INT) leftticketsnumber; 10 11 @ end

Create a person class that has an ID attribute that complies with the ticketdelegate protocol.

1 # import <Foundation/Foundation. h> 2 # import "ticketdelegate. H "3 4 @ interface person: nsobject 5 6-(void) buyticket; 7 8 // has a proxy attribute 9 // ID indicates that the proxy class name can be any 10 // but must comply with the ticketdelegate Protocol 11 @ property (nonatomic, retain) ID <ticketdelegate> delegate; 12 13 @ end
1 # import "person. H "2 3 @ implementation person 4 5 // buy movie tickets 6-(void) buyticket 7 {8 // ask the agent to buy a ticket (inquire about the fare and the remaining number of tickets) 9 double price = [_ delegate ticketprice]; 10 int number = [_ delegate leftticketsnumber]; 11 12 nslog (@ "via proxy, fare = % F, % d tickets left", price, number ); 13} 14 15-(void) dealloc16 {17 [_ delegate release]; 18 [Super dealloc]; 19} 20 @ end

Create a delegate class that complies with the ticketdelegate protocol and implements the ticketprice and leftticketnumber methods.

1 #import <Foundation/Foundation.h>2 #import "TicketDelegate.h"3 4 @interface Agent : NSObject <TicketDelegate>5 6 @end
1 # import "agent. H "2 3 @ implementation agent 4 5 // the remaining number of votes 6-(INT) leftticketsnumber 7 {8 //... run cinema \ in person or call 9 10 return 1; 11} 12 13 // how much is each ticket 14-(double) ticketprice15 {16 //... run cinema \ or call 17 return 1000; 18} 19 @ end

In use, assign the agent object to the ID attribute of the person object, and then call the agent object method to buy tickets.

1 # import <Foundation/Foundation. h> 2 # import "person. H "3 # import" agent. H "4 int main (INT argc, const char * argv []) 5 {6 // person 7 person * P = [[person alloc] init]; 8 // agent 9 Agent * A = [[Agent alloc] init]; 10 11 // set the agent 12 p. delegate = A; 13 14 // person planning to watch movies 15 [p buyticket]; 16 17 [A release]; 18 [P release]; 19}

The proxy mode is to delegate other class methods that comply with the same protocol to implement their own methods.

Protocol Summary:

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.
Generally, they are defined in separate files.

 

Dark Horse programmer-block code 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.