1): block learning Summary
Block: used to save a piece of code.
1. Block features:
1> block encapsulates a piece of code that can be executed at any time
2> A block can be used as the return value of a function or function, and it can contain input parameters or return values.
3> Apple's official recommendation is to use multiple blocks as much as possible, which can be used in multithreading, asynchronous tasks, set traversal, set sorting, and animation transfer.
4> block can generate a code to save it when the program is running.
2. Block flag: ^
3. Block is similar to function
1> code can be saved
2> Return Value
3> Parameters
4> the call method is the same.
4. Define block variables (no parameters)
1> myblock is the variable name, and the () following it indicates there is no form parameter
Void (^ myblock) () = ^ (){
Nslog (@"");
};
2> parentheses can be omitted if no parameters exist.
Void (^ myblock) () = ^ {
Nslog (@"");
};
3> use the block variable to call the internal code of the block.
Myblock ();
5. Define the INT (* p) (INT, INT) pointer to the function. Block is generally used to replace things that can be done with pointers to the function.
6. Define block variables (with parameters)
1> the return value is int, which receives two int-type parameters.
INT (^ sumblock) (INT, INT) = ^ (INT N1, int N2 ){
Return N1 + N2;
};
2> call
Int A = sumblock (3, 4 );
3> output
Nslog (@ "% d", );
7. Use typedef to define the pointer type to the Function
Typedef int (* sump) (INT, INT );
8. Use typedef to define the block type
1> typedef int (^ myblock) (INT, INT );
2> Use block to calculate the sum of two values
Myblock sumblock = ^ (int A, int B ){
Return A + B;
};
Summary
What block needs to know
1. How to define block Variables
INT (^ sumblock) (INT, INT );
Void (^ myblock );
2. How to Use block to encapsulate code
1> Parameters
^ (Int A, int B ){
Return A-B;
}
2> no parameters
^ (){
Nslog (@"");
}
3. block access to external variables
1> the block can access external variables.
2> by default, external local variables cannot be modified inside the block.
3> Add _ block to the local variable, which can be modified inside the block.
4. Use typedef to define the block type
Typedef int (^ myblock) (INT, INT); you can use myblock to define block variables later.
1> define variables before assigning values
Myblock B1, B2;
B1 = ^ (int A, int B ){
Return A + B;
};
2> define variables and assign values at the same time
Myblock B3 = ^ (int A, int B ){
Return A + B;
};
Ii) protocol learning Summary
1. defined a protocol called myprotocol
1> @ protocol is only used to declare the method. After declaring the method, wait for the class to implement it.
2> as long as the class complies with this Protocol, it means that this class has all the method declarations of this Protocol.
3> you can add a keyword to this method to restrict implementation of this method. @ required indicates that it must be implemented. If it is not implemented, a warning is sent. By default, @ optional does not implement this method.
4> nsobject is a basic protocol.
Protocol myprotocol
@ Required: Required
-(Void) test;
@ Optional
-(Void) Test2;
@ End
2. Agreement compliance
1> only one class complies with a certain protocol and has all the method declarations in this Protocol.
2>: Inherit the parent class
3> <> compliance with the Agreement
4> You can abide by multiple protocols and separate them with commas before <>.
@ Interface person: nsobject
@ End
5> require obj3 to keep objects in compliance with myprotocol
Nsobject * obj3 = [[nsobject alloc] init];
6> indicates that the object must comply with this Protocol when it is set using the set method.
@ Property (nonatomic, strong) id obj;
3. Agreement compliance
An agreement that complies with another agreement can have all the methods of this agreement.
@ Protocol myprotocol3
4. protocol specifications
1> tell the compiler that these two guys are a protocol that can replace the Import
2> in this way, the. M file must contain the. h header file of the Protocol.
3> follow the principles, write the Protocol first, and then write the header file to improve performance.
@ Protocol myprotocol2;
@ Protocol myprotocol3;
Summary
1. Protocol definition
@ Protocol name
// 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)
Requires implementation. If not, a warning is reported.
2> @ optional
Implementation is not required, so no warning is given.
4. When defining a variable, restrict the objects stored in the variable to comply with a certain protocol.
1> class name <protocol name> * variable name;
2> id <protocol name> variable name;
3> nsobject * OBJ;
4> id obj;
If the protocol is not followed, the compiler will warn
5. the attribute declared in @ property can also be used as a compliance protocol restriction.
1> @ property (nonatomic, strong) class name <protocol name> * attribute name
2> @ property (nonatomic, strong) ID <protocol name> attribute name
3> @ property (nonatomic, strong) dog * dog;
4> @ property (nonatomic, strong) ID dog;
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. Classification can be defined in a separate. h file or in the original class.
1> generally, they are defined in a separate file.
2> classes defined in the original class only need to be able to understand the syntax
8. Base protocols
1> nsobject is a base class, the most fundamental and basic class. Any class will eventually inherit from it.
2> In fact, there is also a protocol named nsobject, which is a base protocol and the most fundamental and basic protocol.
3> the nsobject Protocol declares many basic methods, such as description, retatin, and release.
4> we recommend that each protocol comply with the base agreement.
Protocol proxy
1> just tell the compiler that the button is a class
@ Class button;
2> define an agreement and comply with the Base Agreement
@ Protocol buttondelegate
3> the idea of the listener. When a button is clicked, it indicates which button is clicked.
-(Void) onclick :( button *);
@ End
4> make the button class comply with the Protocol
@ Interface button: nsobject
-(Void) Click;
5> declared attributes comply with this agreement
@ Property (nonatomic, retain) ID delegate;
@ End
@ Implementation button
6> prevent unrecognized selector sent to instance 0x100114f10. This method is not implemented or is not available. check whether this method is implemented.
-(Void) Click
{
If _ delegate implements onclick: method, this method is called.
If ([_ delegate respondstoselector: @ selector (onclick :)])
{
When the button is clicked, the listener should be notified, and the listener should be notified of which button is clicked.
[_ Delegate onclick: Self];
}
Else
{
Nslog (@ "The Listener does not implement onclick: Method ");
}
}
@ End
Block as proxy callback
# Import
1> just tell the compiler that the button is a class
@ Class button;
2> define the block type with typedef and no return value
Typedef void (^ mybutton) (Button *);
@ Interface button: nsobject
3> define a setter and getter that automatically generate the mybutton type and generate the member Variable _ BTN.
@ Property (nonatomic, assign) mybutton BTN;
-(Void) Click;
@ End
@ Implementation button
4> method implementation: Use the _ BTN variable to call the internal code of the block.
-(Void) Click
{
Call Block
_ BTN (Self );
}
@ End
Int main ()
{
@ Autoreleasepool {
Button * B = [[[Button alloc] init] autorelease];
Call the setter Method
B. BTN = ^ (Button * BTN ){
Nslog (@ "click this-% @ button", BTN );
};
Call Method
[B click];
}
Return 0;
}
Supplement: judge whether Stu complies with this agreement
Student * Stu = [[STUDENT alloc] init] autorelease];
If ([STU conformstoprotocol: @ protocol (Study)])
{
Nslog (@ "% @ compliant with this Protocol", Stu );
}
Black Horse programmer _ objective-C Block, protocol learning notes