- Block
- Block encapsulates a piece of code that can be executed at any time
- A block can be used as a function parameter or as a return value for a function, and it can have either an input parameter or a return value.
- Apple's official proposal to use block as much as possible. In multi-threaded, asynchronous tasks, collection traversal, collection sorting, animation transitions with a lot of
#include <stdio.h>intSumintAintb) { returnA +b;}intMain () {NSLog (@"%d", SUM (5,6)); //How to define a block//void (^myblock) () = ^ () {}; //type (name of the ^block) (parameter type) = (parameter type) {code content}; //use: Similar to function call
int(^sumblock) (int,int) = ^(intAintb) { returnA +b; }; NSLog (@"%d", Sumblock ( One, A)); void(^block) () = ^()//If there are no parameters, the following () can be omitted{NSLog (@"------"); }; Block (); //like a pointer to a function, you can use block instead. int(*p) (int,int) =sum; intP1 = P ( One, A); NSLog (@"%d", p1); return 0;}
- Define the variable at the same time as the declaration, then assign the value
Int (^mysum) (int,int) = ^ (int a,int b) {
return a + B;
};
- You can also first declare the type with a TypeDef, and then define the variable to assign the value
typedef int (^mysum) (int,int);
MySum sum = ^ (int a,int b) {
return a + B;
};
////VIEWCONTROLLER.M//Slowworker////Created by Jierism on 16/7/31.//copyright©2016 year jierism. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet UIButton*Startbutton, @property (weak, nonatomic) Iboutlet Uitextview*Resultstextview, @property (weak, nonatomic) Iboutlet Uiactivityindicatorview*spinner;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}-(NSString *) fetchsomethingfromserver{[nsthread sleepfortimeinterval:1]; return @"Hi There";}-(NSString *) ProcessData: (NSString *) data{[nsthread sleepfortimeinterval:2]; return[Data uppercasestring];}-(NSString *) Calculatefirstresult: (NSString *) data{[nsthread sleepfortimeinterval:3]; return[NSString stringWithFormat:@"Number of Chars:%lu", (unsignedLong) [Data length];}-(NSString *) Calculatesecondresult: (NSString *) data{[nsthread sleepfortimeinterval:4]; return[Data stringbyreplacingoccurrencesofstring:@"E"Withstring:@"e"];}-(Ibaction) DoWork: (ID) sender{Self.resultsTextView.text=@""; NSDate*starttime =[NSDate Date]; //Click the Post button to turn off the statusself.startButton.enabled =NO; //to rotate the spinner[Self.spinner startanimating]; //use Dispatch_get_global_queue (1. Specify priority, 2. Currently not used as 0) function to crawl a global queue that already exists and is always availabledispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); Dispatch_async (Queue,^{nsstring*fetcheddata =[self fetchsomethingfromserver]; NSString*processeddata =[self processdata:fetcheddata]; //with the Dispatch group (Dispatch group), all code blocks that are asynchronously dispatched through the Dispatch_group_async () function are set to loose to execute as quickly as possible. If possible, distribute them to multiple threads concurrently (concurrently).__block NSString *Firstresult; __block NSString*Secondresult; dispatch_group_t Group=dispatch_group_create (); Dispatch_group_async (group, queue,^{Firstresult=[self calculatefirstresult:processeddata]; }); Dispatch_group_async (group, queue,^{Secondresult=[self calculatesecondresult:processeddata]; }); //use Dispatch_group_notify () to specify an additional block of code that will be executed when all blocks of code in the group are finished running. Dispatch_group_notify (group, queue, ^{nsstring*resultssummary = [NSString stringWithFormat:@"first:[%@]\nsecond:[%@]", Firstresult,secondresult]; //call the dispatch function to pass the work back to the main threadDispatch_async (Dispatch_get_main_queue (), ^{Self.resultsTextView.text=resultssummary; Self.startButton.enabled=YES; [Self.spinner stopanimating]; }); NSDate*endtime =[NSDate Date]; NSLog (@"completed in%f seconds", [EndTime timeintervalsincedate:starttime]);//Reduced run time }); });}@end
Proficient in iOS development-block use with multithreading