Block application description, block Application

Source: Internet
Author: User
Tags prototype definition

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];
}

Related Article

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.