iOS4.0以后可以使用block技术。
声明一个block:
int multiplier = 7; int (^myBlock)(int) = ^(int num) {
return num * multiplier; };
block可以使用和它相同范围的变量。
声明一个block变量以后,可以想普通函数一样使用该block。
使用__block声明的变量,在block中可以修改。未使用__block声明的变量,在block中只读。
block的优势:1.block可以将当前执行的代码和回调写在一起,更原子性,例如:并且block可以作为函数的参数;2.block可以访问本地变量,例如:直接访问本地变量(如果需要再block中修改,变量需要使用__block关键字声明),在block中做网络请求,将请求返回的数据赋值给上下文中的变量。
在block中可能会有五种变量类型:
标准的变量: 1.全局变量,包括用static声明的变量;2.全局方法;3.同一代码段的本地变量或参数;
Block中可以使用的另外两种变量:1.使用__block声明的变量,该变量在block中是可被修改了,如果相关block被拷贝到heap了,那么该变量是被保护的。
2.const import(我觉得这个就是const常量)。
五种变量类型在block中对应的访问规则:
1.全局变量:可访问。
2.参数:可访问(就像普通函数的参数一样)。
3.临时变量:只读(如果有嵌套block,那么离变量最近的block可以访问该变量)。
4.使用__block声明的变量:在block中是可以修改的,并且会将修改结果反射到原变量(也就是说,在block中使用的是该变量的引用)。
5.block内部声明的临时变量:就像普通函数内的临时变量一样。
如果在一个block中 通过传引用的方式使用实例变量的话,那么该block对self是强引用的关系;
如果在一个block中 通过传值的方式使用实例变量的话(非引用类型:int,float等),那么该block对该实例变量是强引用关系。举例:函数内部的临时变量。
Copying Blocks
Typically, you shouldn’t need to copy (or retain) a block. You only need to make a copy when you expect the block to be used after destruction of the scope within which it was declared. Copying moves a block to the heap.
You can copy and release blocks using C functions:
Block_copy() ;
Block_release();
To avoid a memory leak, you must always balance a Block_copy() with Block_release().
Patterns to Avoid
A block literal (that is, ^{ ... }) is the address of a stack-local data structure that represents the block. The scope of the stack-local data structure is therefore the enclosing compound statement, so you should avoid the patterns shown in the following examples:
void dontDoThis() {
void (^blockArray[3])(void); // an array of 3 block references
for (int i = 0; i < 3; ++i) {
blockArray[i] = ^{ printf("hello, %d\n", i); };
// WRONG: The block literal scope is the "for" loop.
}
}
void dontDoThisEither() {
void (^block)(void);
int i = random():
if (i > 1000) {
block = ^{ printf("got i at: %d\n", i); };
// WRONG: The block literal scope is the "then" clause.
}
// ... }
Debugging
You can set breakpoints and single step into blocks. You can invoke a block from within a GDB session using invoke-block, as illustrated in this example:
If you want to pass in a C string, you must quote it. For example, to pass this string into the doSomethingWithString block, you would write the following:
官方文档里的一段话,说明了什么是block:
Block Functionality
A block is an anonymous inline collection of code that:
Has a typed argument list just like a function
Has an inferred or declared return type
Can capture state from the lexical scope within which it is defined
Can optionally modify the state of the lexical scope
Can share the potential for modification with other blocks defined within the same lexical scope
Can continue to share and modify state defined within the lexical scope (the stack frame) after the lexical scope (the stack frame) has been destroyed
You can copy a block and even pass it to other threads for deferred execution (or, within its own thread, to a runloop). The compiler and runtime arrange that all variables referenced from the block are preserved for the life of all copies of the block. Although blocks are available to pure C and C++, a block is also always an Objective-C object.
原文:http://www.cnblogs.com/xiaxlsblog/p/3577716.html