First, Block The concept of:
Nickname: code snippets, blocks, closures, is the Apple company added to OC in the language.
function: Saves a piece of code while the program is running, and this code can be passed.
application: Used for communication between objects.
second, block The syntax:
Similar to the syntax of a function pointer, you set the block 's return value and the number and type of parameters.
(1) Definition:
block variablewithout parameter return value :void (^myblock) (void)
a block variable int (^sumblock) (int, int) with a parameter that returns a value ;
(2) Assign value:
Myblock = code in ^{//block}; Sumblock = ^ (int a, int b) { return a + b;};
(3) Call:
Myblock (); Sumblock (3,4); If the argument needs to specify an argument.
typedef simplifies the writing of block types: The first letter of a type name requires capital: typedef void (^myblock) (void); Defines the type alias myblock typedef int (^athematicblock) (int, int); Define type aliases Athematicblock//You can use type aliases to define variables Myblock block1; Athematicblock Block2;
third, block inside to access the variables outside:
By default , external defined local variables cannot be modified inside the block .
Add the __block keyword to the local variable, which can be modified inside the block.
Four,block to achieve inter-object communication: (Nanny to bathe children)
1. determine where to call block and where to pass the block
where the event occurs (the child is dirty), It is where the block is recalled
where the event responds (bathe the child), where it is implemented and passes the block (to the child) ( flexible handling )
2. call block 's class child
you should define a block property to set the copy 's memory management policy.
and to call block when an event occurs
3. passing block class Nanny
defines a block chunk in which the code in this block responds to this event.
Assign this block to The instance object (child) that called block
Example: Set a block property in a child class, and when the child is dirty, invoke the method of child-washing implemented in the nanny, and finally in main let the nanny realize the method of washing the child
Child.h
@interface Child:nsobject@property Nsinteger Cleanvalue; Cleanliness of @property Nsinteger happyvalue; Happiness//-(void) Bathechild: (Child *) child;//defines the block type alias, which mimics the Declaration of the Protocol method typedef void (^handleblock) (child *);// Define a block attribute (handleblock type, name Washblock) @property (copy, nonatomic) Handleblock washblock;-(instancetype) Initwithcleanvalue: (Nsinteger) Cleanvalue happyvalue: (Nsinteger) Happyvalue; @end
Child.m
#import "Child.h" @implementation child-(Instancetype) Initwithcleanvalue: (Nsinteger) Cleanvalue Happyvalue: ( Nsinteger) Happyvalue {self = [super init]; if (self) { Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (timeraction :) Userinfo:nil Repeats:yes]; _cleanvalue = Cleanvalue; _happyvalue = Happyvalue; } return self;} -(void) Timeraction: (Nstimer *) timer { _cleanvalue--; _happyvalue--; NSLog (@ "Clean:%li, Happy:%li", _cleanvalue, _happyvalue); When the cleanliness of the child drops a certain limit, inform the babysitter to bathe him if (_cleanvalue = =) { //call block to deal with the child's Dirty event. //Determines if the call to block if (self.washblock) { self.washblock (self); }}} @end
Nanny.h
@class Child, @interface nanny:nsobject-(void) Washbaby: (Child *) child; @end
Nanny.m
#import "Nanny.h" #import "Child.h" @implementation nanny-(void) Washbaby: (Child *) Child { Child.washblock = ^ (Child *child) { NSLog (@ "The child is dirty, the babysitter washes the child in Vain"); Child.cleanvalue = +;} ;} @end
Main
#import "Child.h" #import "Nanny.h" #import "Grandma.h" #import <foundation/foundation.h>int main (int argc, const char * argv[]) { @autoreleasepool { Nanny *nanny = [[Nanny alloc] init]; Child *child = [[Child alloc] initwithcleanvalue:100 happyvalue:100]; [Nanny Washbaby:child]; [[Nsrunloop Currentrunloop] run]; } return 0;}
Block of communication between iOS development ———— objects