IOS development-Object-C Block implementation
We can regard Block as a closure function, which can access external variables and local variables, but the external variables cannot be modified by default. You can use it for callback, which is more intuitive than using a proxy (Delegate. By the way, many apple APIs use blocks. I. basic definition of Block the basic syntax of Block (also detailed): returnType (^ blockName) (params) = ^ returnType (params) {// code ...}; explanation: return type (^ Block name) (Block parameter) = ^ return type (Block parameter) {put code here}, for example: int (^ myBlock) (int num1, int num2) = ^ int (int num1, int num2) {return 100 ;}; if your Block does not need to return types and parameters, You Can abbreviated it: void (^ myBlock2) () = ^ () {}; or void (^ myBlock2) (void) = ^ void (void) {}; return type or parameter, if not, use "void" instead. You can also delete the () after ^ on the right of the equal sign, that is, void (^ myBlock2) () = ^ {}; is it very concise? You can also define a Block function first, but do not write the function implementation. We can write the implementation of a specific function later, like this: void (^ myBlock2) (void ); myBlock2 = ^ {}; 2. Define the Block in the method as the method definition. Different from the above, the Block name does not need to be written in the declaration, but is later, like this:-(void) getWtihBlock :( void (^) () block {// code... // remember to call block ();} usage: [self getWtihBlock: ^ {NSLog (@ "sdf") ;}]; The following is a detailed example, and wrote the note:/*** append its own string N times (add a newline \ n before each copy) ** @ param string * @ param count append times * @ param stringBlock target Block, where the str Parameter Number of result strings * // Block can also be defined in the method, but the Block name does not need to be defined. // many APIs developed by IOS also use Block, block animation like UIView-(void) getStrWithString :( NSString *) string CopyCount :( int) count resultString :( void (^) (NSString * str )) stringBlock {NSMutableString * newString = [NSMutableString stringWithString: string]; for (NSUInteger I = 0; I <count; I ++) {NSUInteger len = [string length]; NSString * insertString = [NSString stringWithFormat: @ "\ n % @ ", String]; [newString insertString: insertString atIndex: len];} // call the block to input the newString stringBlock (newString: blockObject * block = [[BlockObject alloc] init]; [block getStrWithString: @ "Garvey" CopyCount: 3 resultString: ^ (NSString * str) {// str is the processed result NSLog (@ "str is % @", str) ;}]; sometimes complicated Block syntax will make the function declaration hard to read, therefore, typedef is often used to create a new type for the Block. Typedef void (^ ResultBlock) (NSString * str); the method is changed to:-(void) getStrWithString2 :( NSString *) string CopyCount :( int) count resultString :( ResultBlock) stringBlock; let's compare before and after typedef: // use prefix-(void) getStrWithString :( NSString *) string CopyCount :( int) count resultString :( void (^) (NSString * str )) stringBlock; // after use-(void) getStrWithString2 :( NSString *) string CopyCount :( int) count resultString :( ResultBlock) StringBlock; Note: The usage is the same, but the definition is simplified. If you have been using a proxy (Delegate) for method callback, you can now try to use the Block function.