Block is an extension of the C language. An anonymous function with an automatic variable (local variable). (Functions without a name)
Non-anonymous function: int func (int count), (a function named Func is declared) using: int result =func (10);
Anonymous functions:
Block Syntax:
- ^ return value type parameter list expression
- ^int (int count) {return count+1};
- Omit return value:
- ^ (int count) {return count+1};
- Omit the return value and the parameter list:
- ^ () {return count+1}; or ^{printf ("1223");}
- To assign a block to a block type variable:
return value type (^ block name) (parameter type) =^ argument list expression
Int (^BLK) (int) =^ (int count) {return count+1;};
Call the Block object variable as you call the function:
int result = Myblock (4); Result is 28.
- Use block in function parameters and return values
- function parameter: void func (int (^blk) (int)) {
}
- return value: Int (^func ())) (int) {
return ^ (int count) {return count+1}
}
2. Using typedef simplification (defining aliases)
Definition: typedef int (^blk_t) (int);
- Original: void func (int (^blk) (int)) {
- Simplified: void func (blk_t blk)
- Original: Int (^func ())) (int) {
- Simplification: blk_t func ()
Originally:typedef int (^myblock) (int, int);
Int (^minusblock) (int, int) = ^ (int a, int b) {
return a-B;
};
Int (^multiblock) (int, int) = ^ (int a, int b) {
return a * b;
};
The following code is more concise
myblock minusblock = ^ (int a, int b) {
return a-B;
};
myblock multiblock = ^ (int a, int b) {
return a * b;
};
- (1) In a class, define a block variable, just like defining a function;
- (2) blocks can be defined within a method or can be defined outside the method;
- (3) The code in its {} body is executed only when the block is called;
Several features of block:
- Interception of automatic variable values
Int dmy=256;
Int val=10;
Const Char *fmt= "val=%d\n";
void (^BLK) (void) =^printf (fmt,val);};
val=2;
Fmt= "These values were changed. Val=%d\n ";
Blk ();
Return 0;
}
Output result: val=10;
2. Modify the external variable ( add _block specifier )
Define a block inside a method
int x = 100;
void (^sumxandyblock) (int) = ^ (int y) {
x = X+y;
printf ("New x value is%d", x);
};
Sumxandyblock (50);
Compilation error occurred: Error:variable is not assigning (missing __block type)
int x = 100 is given at this time, and the statement is preceded by the __block keyword.
__block int x = 100;
This allows you to modify the external variables in the block's {} body.
Block Storage domain:
The structure in which the program occupies memory distribution:
Stack Area ( Stack ): Automatically assigned by the system, the general storage function parameter value, local variable value and so on. Automatically created and disposed by the compiler. Its operation is similar to the stack in the data structure, that is, the principle of last-first-out, advanced-out.
Heap Area ( Heap ): Typically applied by programmers and indicated by size, and eventually released by the programmer. If the programmer does not release, the program may end up being recycled by the OS.
Global Zone / static zones : As the name implies, global variables and static variables are stored in this area. Only initialized global variables and static variables are stored in a block, and uninitialized global variables and static variables are stored in a block. Released by the system after the program is finished.
literal constant Area : This area mainly stores string constants. Released by the system after the program is finished.
Program code Area: This area mainly stores the binary code of the function body
int a = 0; Global initialization Zone
Char *p1; Global uninitialized Zone
Main {
int b; Stack
Char s[] = "ABC"; Stack
Char *p2; Stack
Char *p3 = "123456"; 123456\0 in the constant area, p3 on the stack
static int c = 0;//global static initialization area
P1 = (char *) malloc (10);
P2 = (char *) malloc (20); The allocated 10 and 20 bytes of the area are in the heap area.
strcpy (P1, "123456"); 123456\0 in the constant area, the function is to copy the string "123456" into a 10-byte heap area that is placed in the P1 request.
The "123456" pointed to by P3 and "123456" here may be optimized by the compiler as an address.
Use of block:
1.Block Transmit Value
Pass the textfield.text of the B interface to the label of the A interface
Page A: rootviewcontrollers b page: detailviewcontrollers
Page B: Detailviewcontroller file
#import <UIKit/Uikit.h>
typedef void (^detailblock) (NSString *);//block alias. and parameter write parameters that need to be passed in the parameter list
@interface Detailviewcontroller:uiviewcontroller
@property (nonatomic, copy) Passingvalueblock passingvalue;//set block properties (note using copy)
@property (Weak, nonatomic) Uitextfield *inputtf;
@end
-(Ibaction) Btnaction: (ID) Sender {
Determine if block is empty
if (self. passingvalue) {
Self. Passingvalue (Self.inputTF.text);
}
[Self.navigationcontroller Popviewcontrolleranimated:yes];
}//Click the button to the A interface
Rootviewcontroller.m
@property (Strong, nonatomic) UILabel *textlabel;
-(void) Handlebutton: (nsstring*) sender{
Detailviewcontroller *detailviewcontroller = [[Detailviewcontroller alloc]init];
Detailviewcontroller.passingvalue=^ (nsstring* str) {
Self. textlabel.text= str;}
[Self.navigationcontroller Pushviewcontroller:detailviewcontroller Animated:yes];
}
2.block Avoid circular references
Since many of our actions lead to the copy of block, when the block is copied, it generates a strong reference to the object used in the block (under ARC) or a reference count plus one (NON-ARC).
If you encounter this situation:
@property (nonatomic, ReadWrite, copy) Completionblock Completionblock;
Self.completionblock = ^ {
if (self.success) {
Self.success (Self.responsedata);
}
}
};
The object has a block property, but the block attribute also references the other member variables of the object, then the variable itself is strongly applied, then the variable itself and his own block property form a circular reference. Under Arc it needs to be modified like this:
@property (nonatomic, ReadWrite, copy) Completionblock Completionblock;
__weak typeof (self) weakself = self;
Self.completionblock = ^ {
if (weakself.success) {
Weakself.success (Weakself.responsedata);
}
};
Note 1:ios4.3 The previous version is replaced with __unsafe_unretained __weak
NOTE 2: If you are in a NON-ARC environment, replace __weak with __block.
The Block for iOS learning