Block Introduction
Block is added after iOS 4.0 and is heavily used in the new iOS API. Block is an anonymous block of code that can be passed as a parameter to other objects and get the return value. In essence, blocks are similar to other common variables, except that their stored data is a function body. Block is not just a patent for objective-c, but a new feature that can be applied to the language level of C, C + +, and objective-c. By using block, a developer can pass a piece of code as an argument to a function as a value. At the same time, blocks is also an object of objective-c that can be added to a collection such as Nsarray or nsdictionary like any other object.
- Block syntax, which is essentially an anonymous function. Very similar to function pointers
- 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.
Block Basic use
1> How to define a block variable
int (^sumblock) (intintvoid (^int (^myblock) (int) = ^ (int return num *multiplier};
2> How to use block encapsulation code
^ (intint b) { return A- b;}; ^() { NSLog (@ "----------");}; ^ { NSLog (@ "----------");};
3> block access outside variables
* External variables can be accessed inside the block
* By default, the outside local variables cannot be modified inside the block
* Add __block keyword to local variable, this local variable can be modified inside block
* To add the static keyword to the local variable, this local variable can be modified inside the block
4> defining block types with typedef
int (^myblock) (intint// can later use Myblock this type to define the block variable myblock block; Myblock B1, B2; = ^ (intint b) { return A- b;}; = ^ (intint b) { return A- b;};
Details and nature of block use
1.block is actually a pointer to the struct body
2. The compiler will generate the corresponding function for the block's internal code
Memory management of Block
1. By default, block memory is in the stack
* It does not do anything to the referenced object
2. If a copy operation is made to the block, the block's memory will be in the heap
* It will do a retain operation on the referenced object
* Non-arc: If the referenced object is __block decorated, the retain operation is not done
* ARC: If the referenced object is __unsafe_unretained\__weak decorated, the retain operation is not done
here is a personal favorite about block memory management articles, interested in the study can look at: memory management of Block
Block Usage Note
1. Before using block, the block hands should be sentenced to empty processing.
No empty direct use, once the pointer is empty directly generates a crash.
if (! self.isonlynet) { if// after the use of block will be the first to be sentenced to empty processing return; } ID data = [Nskeyedunarchiver unarchiveobjectwithfile:[self Favoritefile]]; if class ]]) { succblock (data,yes); } Else { succblock (nil,yes); }}
2. Before adding self or member variable to block, the user must first change to __weak
3. In a multithreaded environment (where the weakself in a block is likely to be refactored), you need to turn self into a strong pointer, avoiding the Self object being refactored when running to a critical step.
__weak __typeof (self) weakself == ^(afnetworkreachabilitystatus status) { = weakself; = status; if (strongself.networkreachabilitystatusblock) { strongself.networkreachabilitystatusblock (status); }};
Block usage Scenarios
(Pending completion)
The Magical magic of block
(Pending completion)
Using block for chained programming
(Pending completion)
Block summary
(Pending completion)
Explore block usage in iOS