Block application description, block Application
I. Block Definition
Block can be understood as a function pointer (that is, a pointer pointing to a function)
ReturnType (^ blockName) (parameter list) = ^ (parameter list) {code block };
Note:
ReturnType: return type of the block, which can be defined as void;
BlockName: block name, which can be named according to different variable/function naming rules;
Parameter list: defines the type of block parameters. Multiple values can be input or no parameters are required;
Example:
// Define a Block with no parameters and no return values
Void (^ printBlock) () = ^ (){
Printf ("no number ");
};
PrintBlock ();
Void (^ myBlock) (NSString *); // block Declaration
-(Void) viewDidLoad
{
// Implement block in the code
MyBlock = ^ (NSString * text ){
NSLog (@ "my text = % @", text );
};
// Call the implementation of the block Function
Int nCount = myCount (10, 5 );
NSLog (@ "count = % zi", nCount );
}
// Block function prototype Definition
Int (^ myCount) (int a, int B) = ^ (int a, int B)
{
Return a + B;
};
Ii. Passing blocks as function parameters
ReturnType (^ blockName) (parameter list)
Note:
ReturnType: return type of the block, which can be defined as void;
BlockName: block name, which can be named according to different variable/function naming rules;
Parameter list: defines the type of block parameters. Multiple values can be input or no parameters are required;
Actual declaration method:
+ (Void) updateImage :( void (^) (NSInteger status, NSDictionary * res) block;
// Change the network Request Method
-(Void) ModifyRequestMode :( void (^) () block
{
// Cancel all tasks and put them in the Global queue with the highest priority
Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
ZLBAFHTTPClient * httpClient = [ZLBAFHTTPClient shareClient];
// Cancel all network request tasks in the queue
[HttpClient cancelAllTasks];
// Cancel all tasks in the queue
[HttpClient. workQueue cancelAllOperations];
// Re-match the correct CDN Node
[[ZLBGlobal sharedGlobal] changeNetwork: ^ {
Block ();
}];
});
}
-(RACDisposable *) subscribeNext :( void (^) (id x) nextBlock completed :( void (^) (void) completedBlock {
NSCParameterAssert (nextBlock! = NULL );
NSCParameterAssert (completedBlock! = NULL );
RACSubscriber * o = [RACSubscriber subscriberWithNext: nextBlock error: NULL completed: completedBlock];
Return [self subscribe: o];
}