Dark Horse programmer--oc Language Basic Grammar Knowledge (V)

Source: Internet
Author: User

Block: Used to save a piece of code, with ^

 1, block features:
  1> Block encapsulates a piece of code that can execute   at any time;
  2> block can be used as the return value of a function or function. And it can take input parameters or return values
  3> Apple's official recommendation is to use blocks as much as possible, in multi-threaded, asynchronous tasks, set traversal, collection sorting, A lot of the
  4> block can produce a code saved in a single run while the program is running
 2, Block Flags: ^
 3, block is like function
  1> can save code
  2> return value
  3> tangible parameter
  4> Call as
 4, define a block variable (no formal parameter)
  1> myblock is the variable name, and the following () represents no formal parameter
    void (^myblock) () = ^ () {
   nslog (@ "");
   };
  2> No formal parameter can omit the following parentheses
  void (^myblock) () = ^{
       NSLog (@ "");
  };

3> using block variables to invoke code inside block
Myblock ();

5. Define pointer int (*P) (int, int) pointing to the function; Things that can be done with pointers to functions are usually replaced with blocks.
6. Define block variables (physical parameters)
1> return value is int, receives two parameters of type int
Int (^sumblock) (int,int) = ^ (int n1, int n2) {
return n1+n2;
};
2> Call
int a = Sumblock (3,4);
3> output
NSLog (@ "%d", a);
7. Use typedef to define pointer types that point to functions
typedef int (*sump) (int,int);
8. Define block type with typedef
1> typedef int (^myblock) (int,int);
2> calculates the value of two values with a block
Myblock Sumblock = ^ (int a, int b) {
return a + B;
};

Summary
what the block needs to know.
1. How to define the block variable
Int (^sumblock) (int,int);
void (^myblock);
 
2. How to use block encapsulation code
1> Tangible Parameters
^ (int a, int b) {
return a-B;
 }
2> No formal parameters
 ^() {
NSLog (@ "");
 }
 
3. Block access outside variables
1> inside the block can access the outside variables
2> by default, the outer local variables cannot be modified inside the block
3> add __block to local variables, this local variable can be modified inside the block

4. Define block type with typedef

typedef int (^myblock) (int, int); You can then use this type of Myblock to define the block variable

1> define variables and assign values first
Myblock b1,b2;

B1 = ^ (int a, int b) {
return a+b;
};

2> defining variables to assign values at the same time
Myblock B3 = ^ (int a, int b) {
return a+b;
};

Protocol
1. Define a protocol called MyProtocol
1> @protocol is only used to declare methods, after declaring the method and waiting for the class to implement
2> as long as the class complies with this protocol, it means that this class has all the method declarations of this Protocol.
3> to this method can add a keyword to constrain this method to implement, @required representative must implement, do not implement will send a warning, the default is that this @optional can not be implemented
4> NSObject, it is a base protocol, the most fundamental and basic protocol
Protocol MyProtocol
@required must implement
-(void) test;
@optional can not be implemented
-(void) test2;
@end

2. Compliance with the agreement
1> there is only one class that complies with one protocol and has all the method declarations in this agreement
2>: Inheriting the parent class
3> <> Compliance Agreement
4> can comply with multiple protocols, separated by commas before the <> protocol
@interface Person:nsobject
@end
5> requires obj3 save objects to follow MyProtocol
NSObject *obj3 = [[NSObject alloc] init];
6> the object must be subject to this protocol when the object is set using the Set method.
@property (nonatomic, strong) ID obj;

3. Agreement to abide by the agreement
A protocol that complies with another agreement can have all the method statements of the agreement
@protocol MyProtocol3

4, the specification of the agreement
1> tells the compiler that these two guys are an agreement that can replace the import
2> This will include the protocol. h header file in the. m file
3> obey the principle, write the protocol first, and then write the header file when you really want to use it to improve performance
@protocol MyProtocol2;
@protocol MyProtocol3;

Summary
1, the definition of the Agreement
@protocol Agreement 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> Protocol Compliance Agreement
@protocol Agreement name < Other protocol name 1, other protocol name 2>
@end
 
3. Keywords for method declarations in the agreement
1> @required (default)
Required implementation, warning if no implementation occurs
2> @optional
no need to implement, no warning
 
4. When defining a variable, limit the object to which the variable is stored to comply with a protocol
1> class name < protocol name > * variable name;
2> id< protocol name > variable name;
3> nsobject *obj;
4> ID obj;
if the corresponding protocol is not followed, the compiler warns
 
5. The attributes declared in the @property can also be used as a restriction to comply with the agreement
1> @property (nonatomic, Strong) class name < protocol name > * Attribute name
2> @property (nonatomic, strong) id< Protocol name > property 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 it can be defined in a class
1> If this 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, classification can be defined in a separate. h file, can also be defined in the original class
1> in general, are defined in a separate file
2> classes defined in the original class, only requires the ability to read the syntax

8. Base protocol
1> NSObject is a base class, the most fundamental and basic class, and any class will eventually inherit his
2> actually has an agreement, the name is also called NSObject, it is a base protocol, the most fundamental and basic protocol
The 3> NSObject protocol declares many of the most basic methods, such as Description,retatin, release
4> recommends adherence to the base protocol for each protocol

Protocol's agent
1> just tells the compiler that the button is a class
@class Button;
2> defines a protocol and adheres to the base protocol
@protocol buttondelegate
3> listener thought that when a button is clicked it will notice which button was clicked.
-(void) OnClick: (Button *) but;
@end
4> make the button this class abide by the protocol
@interface Button:nsobject
-(void) click;
The attributes in the 5> declaration comply with this Protocol
@property (nonatomic, retain) ID delegate;
@end
@implementation Button
6> prevent unrecognized selector sent to instance 0x100114f10, method is not implemented or does not have this method, to determine if there is no implementation of this method
-(void) Click
{
If _delegate implements the OnClick: method, call this method
if ([_delegate respondstoselector: @selector (OnClick:)])
{
When the button is clicked, you should notify the listener and tell the listener which button was clicked.
[_delegate onclick:self];
}
Else
{
NSLog (@ "Listener does not implement OnClick: Method");
}
}
@end

Block as proxy callback
#import
1> just tells the compiler that the button is a class
@class Button;
2> defines a block type with a typedef with no return value
typedef void (^mybutton) (Button *);

@interface Button:nsobject
3> defines a setter and getter that automatically generates a MyButton type, generating _btn this member variable
@property (nonatomic, assign) MyButton btn;
-(void) click;
@end

@implementation Button
4> method Implementation, call the code inside the block with the _BTN variable
-(void) Click
{
Call Block
_BTN (self);
}
@end

int main ()
{
@autoreleasepool {
button *b = [[[Button alloc] init] autorelease];

Call setter Method
B.BTN = ^ (Button *btn) {

NSLog (@ "Point this-%@ button", BTN);
};

Calling methods
[B click];

}
return 0;
}

Add: Judge Stu have complied with this agreement
Student *stu = [[[[Student alloc] init] autorelease];

if ([Stu Conformstoprotocol: @protocol (Study)])
{
NSLog (@ "%@ complies with this agreement", Stu);
}

Dark Horse programmer--oc Language Basic Grammar Knowledge (V)

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.