Block and protocol in the Dark Horse programmer _oc

Source: Internet
Author: User

First, Block

Block similar to pointers to functions.

1. define the block variable

 #import  <foundation/foundation.h> int  main (int argc, const char * argv[]) {    //defines the block variable, no return value type , there is no formal parameter     void  (^myblock);        //assigns a value to the block variable     myblock=^{        nslog (@ "----------");     };       //call Block    myblock ();        //defines a block variable that returns a value of type int, and assigns a value of     int  (^myblock2 ) (Int,int) =^ (int a,int b)     {         return a+b;       };       //Call     nslog (@ "%d", MyBlock2 (2,3));        return 0;} 

2. Encapsulating the block code

^ (int a,int b) {return a+b; };    ^{NSLog (@ "----------"); };

3.block modifying external variables

&NBSP;   block can access external variables, but by default, block Inside can not modify the outside local variables, add to the local variable __block this keyword can be in block Internal modifications.

int main () {int a=0;        __block int b=9;        Defines a block variable that returns a value of type int, and an assignment of int (^MYBLOCK2) () =^{//a=9; error b=9;    return A;        }; return 0;}


4. using typedef definition block

#import <Foundation/Foundation.h> typedef int (^myblock) (int,int);//This type of block is defined as Myblockint main () {Myblock sum ;    Defines the variable sum=^ (int a,int b) of the Myblock type {return a+b;    }; int i=sum (2,3);       Call Block NSLog (@ "%d", I);    Myblock minus=^ (int i,int j) {return i-j;    };    I=minus (100,12);       NSLog (@ "%d", I); return 0;}



Second,protocol

1. Basic Use
can be used to declare a large stack of methods (cannot declare member variables).
as long as a class adheres to this protocol, it is equivalent to having all the method declarations in this agreement.
as long as the parent class adheres to a protocol, it is equivalent to the subclass being followed.

2.format
the preparation of the agreement
  @protocolAgreement name
    // method Declaration List
   @end

A class adheres to the agreement
@interface : parent < protocol name >
@end

Protocol is only . h of the file

3. Keywords

  the agreement has2keyword to control whether a method is implemented (by default@required), in most cases, the use of communication between programmers
@required: This method must be implemented (if not implemented, the compiler will issue a warning)
@optional: This method is not necessarily implemented

4.Agreement Compliance Agreement
one protocol can comply with multiple other protocols, with commas between multiple protocols , separated
a protocol that complies with other agreements is equivalent to having a method declaration in another agreement
@protocolAgreement name<Protocol1,Protocol2>
@end

5.Base Protocol
NSObjectis a base class, the most fundamental and basic class, and any other class will eventually inherit it .
In fact, there is a protocol, the name is also calledNSObject, it is a base protocol, the most fundamental and basic protocol
NSObjectThe protocol declares many of the most basic methods, such asDescription,retain,Releasewait
It is recommended that every new protocol be followedNSObjectProtocol

6.specify protocol when defining variables

An object of type  nsobject, and to comply with the Nscopying protocol nsobject<nscopying> *obj;//  any OC object, and to comply with the Nscoding protocol id< nscoding> obj2; /* 1. Definition of 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,  agreement name 2>   @end   2>  Protocol Compliance Agreement   @protocol   Agreement name  < Other protocol name 1,  other protocol name 2>   @end   3. The keyword of the method declaration in the agreement  1>  @required   ( Default)     require implementation, if not implemented, issue a warning   2>  @optional     do not require implementation, how can there be no warning    4. When defining a variable, restrict the object that this variable holds to a protocol   class name < protocol name > * variable name; 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 compliance protocol restriction   @property   (Nonatomic, strong)   class name < protocol name >  * Property name;  @property   (nonatomic, strong)  id< agreement 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, and can also be defined in a class  1>  if the protocol is used only in a class, the protocol should be defined in that class   2>   If this protocol is used in many classes, it should be defined in a separate file   7. Classifications can be defined in separate. h and. m files, or in the original class  1>  in general, are defined in separate files   2>  defines the classification in the original class, only requires the ability to read grammar  */


Block and protocol in the Dark Horse programmer _oc

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.