1 , define
(1) block is a data type in OC and is widely used in iOS development
(2) ^ is a unique mark of block
(3) Block implementation code is included in the {}
(4) In most cases, the way inline functions are defined and used
(5) block is somewhat similar to the C language function pointer, but it is more flexible to use.
For example:
void (^demoblock) () = ^ {
NSLog (@ "demo Block");
};
Int (^sumblock) (int, int) = ^ (int x, int y) {
return x + y;
};
Format Description:
(return type) (^ block name) (parameter type) = ^ (parameter list) {code implementation};
If there are no arguments, the () argument list after the equals sign can be omitted
2 , common related questions
A block can use a local variable declared before the definition:
int i = 10;
void (^myblock) () = ^{
NSLog (@ "%d", I);
};
i = 100;//actually no effect
Myblock ();
the output is: Ten
Attention:
(1) when a block is defined , a copy of the contents of the current local variable is created in the block ( copy )
(2) Subsequent changes to the value of the variable will not affect the value in the Block
(3) If you need to keep the value of the local variable in the block, you need to use the __block keyword
(4) After using the __block keyword, you can also modify the value of the variable in the block
3 , as parameter passing
Blocks can be passed directly as parameters:
Nsarray *array = @[@ "Zhang San", @ "John Doe", @ "Harry", @ "Zhao Liu"];
[Array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, BOOL *stop) {
NSLog (@ "%d content is%@", (int) idx, obj);
if ([@ "Harry" Isequaltostring:obj]) {
*stop = YES;
}
}];
Description: Traverses and NSLog () the contents of the array, stopping the traversal when obj is "Harry"
4 , using local variables
When passed as a parameter, block can also use local variables declared before the definition:
int stopindex = 1;
Nsarray *array = @[@ "Zhang San", @ "John Doe", @ "Harry", @ "Zhao Liu"];
[Array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, BOOL *stop) {
NSLog (@ "%d content is%@", (int) idx, obj);
if ([@ "Harry" Isequaltostring:obj] | | idx = = stopindex) {
*stop = YES;
}
}];
note that, by default, variables outside the block are read-only in the block !
BOOL flag = NO;
[Array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, BOOL *stop) {
if ([@ "Harry" Isequaltostring:obj] | | idx = = stopindex) {
*stop = YES;
flag = YES; COMPILATION Error!!!
}
}];
5 , __block Key Words
If you want to modify Block Local variables outside of , need to use __block Key Words
__block BOOL flag = NO;
[Array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, BOOL *stop) {
if ([@ "Harry" Isequaltostring:obj] | | idx = = stopindex) {
*stop = YES;
flag = YES; can now be modified!!!
}
}];
Tip: You can modify the value of a member variable (less) in block code without using the __block keyword.
6 , Passing Objects
How objects are passed into the block
NSString *stopname = @ "Harry";
Nsarray *array = @[@ "Zhang San", @ "John Doe", @ "Harry", @ "Zhao Liu"];
[Array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, BOOL *stop) {
NSLog (@ "%d content is%@", (int) idx, obj);
if ([Stopname isequaltostring:obj] | | idx = = stopindex) {
*stop = YES;
}
}];
To ensure that the code in the block is working properly, the block will automatically strongly reference the Stopname pointer when passing the Stopname pointer to the block.
7 , Block working in the stack area
8 , typedef
You can use TypeDef to define the type of block that is convenient for subsequent direct use
typedef double (^myblock) (double, double);
Myblock area = ^ (double x, double y) {
return x * y;
};
Myblock sum = ^ (double A, double b) {
return a + B;
};
NSLog (@ "%.2f", Area (10.0, 20.0));
NSLog (@ "%.2f", SUM (10.0, 20.0));
Description
(1) typedef is the keyword used to define the type, Myblock is the block type defined
(2) area and sum are two block variables of the Myblock type respectively.
Although typedef can simplify the definition of block, the TypeDef keyword is not frequently used in real-world development.
This is because the block has very high flexibility, especially when it is passed with a parameter, the purpose of using block is to use it immediately.
The official array traversal method is declared as follows:
If you use TypeDef, you need:
(1) typedef void (^enumerateblock) (id obj, Nsuinteger idx, BOOL *stop);
(2)-(void) Enumerateobjectsusingblock: (enumerateblock) block;
The end result is that Enumerateblock has no other use other than defining the type.
9 , Add to Array
Since block is a data type, block can be treated as a special object
#pragma mark defined and added to an array
@property (nonatomic, strong) Nsmutablearray *myblocks;
Int (^sum) (int, int) = ^ (int x, int y) {
return [self sum:x y:y];
};
[Self.myblocks Addobject:sum];
Int (^area) (int, int) = ^ (int x, int y) {
return [self area:x y:y];
};
[Self.myblocks Addobject:area];
#pragma mark calls the block stored in the array
Int (^func) (int, int) = Self.myblocks[index];
return func (x, y);
Ten , Circular References
@property (nonatomic, strong) Nsmutablearray *myblocks;
#pragma mark changes the code to a method that calls self
Int (^sum) (int, int) = ^ (int x, int y) {
return [self sum:x y:y];
};
[Self.myblocks Addobject:sum];
Automatically called when the Mark object is released #pragma
-(void) dealloc
{
NSLog (@ "Demoobj is released");
}
11. Remove Circular references
Local variables are strongly referenced by default, and are freed when they leave the scope they reside in.
You can declare a local variable as a weak reference by using the __weak keyword
__weak demoobj *weakself = self;
If you reference weakself in a block, the block no longer makes a strong reference to self
Int (^sum) (int, int) = ^ (int x, int y) {
return [weakself sum:x y:y];
};
The Block for iOS development