如何在 iOS 5 中使用 ios中block介绍

ios 开发中 怎么使用block 代替代理方法
我的图书馆
ios 开发中 怎么使用block 代替代理方法
声明一个无返回值的block&1typedef&void(^onSearch)(NSString&*searchText);&2.在控制器中头文件.h中定义block,分两步:&1&定义成属性&1&a href="/s?wd=%40property&tn=_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLnWRkPynkP1I9rHN-PWFB0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1TknHmvP1T4rjD1nHbsnWTzPf"&target="_blank"&class="baidu-highlight"&@property&/a&&(nonatomic)&&onSearch&searchB&2&定义一个block的setter方法1-(void)setSearchBlock:(void(^)(NSString&*searchText))&3.在控制器的实现文件.m中来实现12345-(void)setSearchBlock:(void(^)(NSString&*searchText))&block&{&&&&&&&&if&(block)&{&&&&&&&&&&&&&&&&&self.searchBlock&=&&&&&&&&&}}4.在控制器中使用block实现回调. 当点击search按钮时,将search的文本传递给订阅者12345-(void)searchHandler:(id)sender&{&&&&&&&if&(self.searchBlock)&{&&&&&&&&&&&&&&&&self.searchBlock(@"your&search&keyword")&&&&&&&&}}5.在订阅者中,接收过来的参数,并做其它操作1234YourVC&&*vc&=&[YourVC&new];[vc&setSearchBlock:^(NSString&*searchText)&{&&&&&&&&&&&&&NSLog(@"get&search&keyword")&&&&&&&&}]
TA的最新馆藏[转]&[转]&[转]&[转]&
喜欢该文的人也喜欢今天看啥 热点:
IOS block编程指南 5 Block和变量
Blocks and Variables(Block和变量)
This article describes the interaction between blocks and variables, including memory management.
本文讲述了block和变量之间的内在关系,包括内存管理。
Types of Variable(变量类型)
Within the block object&s body of code, variables may be treated in five different ways.
You can reference three standard types of variable, just as you would from a function:
Global variables, including static locals
Global functions (which aren&t technically variables)
Local variables and parameters from an enclosing scope
block对象中的代码中,变量会被五种方法区别对待。 你可以参考三种标准变量,就像你在一个函数中那样: 全局变量,包括静态的本地变量全局函数,(这个并不是技术上的变量)本地变量,和同一个作用域的参数
Blocks also support two other types of variable:
At function level are __block variables. These are mutable within the block (and the enclosing scope) and are preserved if any referencing block is copied to the heap.
const imports.
Finally, within a method implementation, blocks may reference Objective-C instance variables&see Object and Block Variables.
block也支持其他两种类型的变量 在函数级别的_block 变量。这些变量在block中是可变的,同时如果引用变量的block拷贝进堆堆,手动管理内存)内存就会保存 。 常量 最后,随着方法的实现,blocks会引用objective-C的实例变量&&参见: Object and Block Variables。
The following rules apply to variables used within a block:
Global variables are accessible, including static variables that exist within the enclosing lexical scope.
Parameters passed to the block are accessible (just like parameters to a function).
Stack (non-static) variables local to the enclosing lexical scope are captured as const variables.
Their values are taken at the point of the block expression within the program. In nested blocks, the value is captured from the nearest enclosing scope.
Variables local to the enclosing lexical scope declared with the __block storage modifier are provided by reference and so are mutable.
Any changes are reflected in the enclosing lexical scope, including any other blocks defined within the same enclosing lexical scope. These are discussed in more detail inThe __block Storage Type.
Local variables declared within the lexical scope of the block, which behave exactly like local variables in a function.
Each invocation of the block provides a new copy of that variable. These variables can in turn be used as const or by-reference variables in blocks enclosed within the block.
以下规则适用于block中使用的变量: 全局变量可以访问,包括作用域内的静态变量。(这里block有对全局变量的读写权限)传入block中的参数可以访问(就像函数中的参数)。作用域中的栈(自动内存管理)(非静态)变量,被当做常量来使用。 在程序中他们的值被block表达式调用。在嵌套block中,会使用最近的作用域中的变量。(笔者的理解是,嵌套的只能用最近的。PS:这一大段其实就几个字:block中普通变量是只读的)作用域中的_block变量提供的是引用,所以是可变的。 所有的变动都会反映在作用域中,包括同一个作用域内定义了其他的block。更具体的讨论:见The __block Storage Type。block中声明的局部变量,他们的行为很像函数中的局部变量。 block的每次调用都会给变量提供一份新的拷贝。这些在block作用域中的变量可以是常量,也可以做变量(引用的)。
The following example illustrates the use of local non-static variables:
下列例子说明了非静态局部变量变量的使用
int x = 123;
void (^printXAndY)(int) = ^(int y) {
printf(%d %d
printXAndY(456); // prints: 123 456
As noted, trying to assign a new value to x within the block would result in an error:
值得注意的是,试图在块内给block赋值会返回error
int x = 123;
void (^printXAndY)(int) = ^(int y) {
x = x + // error
printf(%d %d
To allow a variable to be changed within a block, you use the __block storage type modifier&see The __block Storage Type.
为了允许在block中修改变量,你需要用 _block 类型存储 ,见:The __block Storage Type。
The __block Storage Type(_block 存储类型)
You can specify that an imported variable be mutable&that is, read-write& by applying the __block storage type modifier. __block storage is similar to, but mutually exclusive of, the register, auto, and static storage types for local variables.
__block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created within the variable&s lexical scope. Thus, the storage will survive the destruction of the stack frame if any copies of the blocks declared within the frame survive beyond the end of the frame (for example, by being enqueued somewhere for later execution). Multiple blocks in a given lexical scope can simultaneously use a shared variable.
As an optimization, block storage starts out on the stack&just like blocks themselves do. If the block is copied using Block_copy (or in Objective-C when the block is sent a copy), variables are copied to the heap. Thus, the address of a __block variable can change over time.
There are two further restrictions on __block variables: they cannot be variable length arrays, and cannot be structures that contain C99 variable-length arrays.
你可以指定一个输入的变量是可变的,(可以读,写),通过使用_block存储类型修饰符。_block很类似,(但是区别于),局部变量的 register,auto,stack,存储类型。 _block变量生存的存储范围,是变量的作用域和所有block和block的拷贝声明的或者创造的作用域。因此呢,如果block的一个拷贝声明在一个没有被销毁的框架里面,存储会在栈内存销毁时不被销毁,直到这个框架结束(举例,被排到某处,延后执行)。同一作用域下的多个block可以共同使用一个变量。 作为优化,block存储从栈开始&&就像block他们自己在做。如果block使用Block_copy(或Objective-C,当block发送出一份拷贝的时候),变量拷贝到堆(手动内存管理)。这样_block变量就可以随时改变地址了。 对于_block变量有两个更进一步的约束:他们不能作为变长数组,不能成为包含C99数组的结构。
The following example illustrates use of a __block variable:
下面的例子说说明了_block变量的使用。
__block int x = 123; //
x lives in block storage
void (^printXAndY)(int) = ^(int y) {
printf(%d %d
printXAndY(456); // prints: 579 456
// x is now 579
The following example shows the interaction of blocks with several types of variables:
这个例子展现了几种类型的block的相互作用
extern NSInteger CounterG
static NSInteger CounterS
NSInteger localCounter = 42;
__block char localC
void (^aBlock)(void) = ^(void) {
++CounterG
++CounterS
CounterGlobal = localC // localCounter fixed at block creation
localCharacter = 'a'; // sets localCharacter in enclosing scope
++localC // unseen by the block
localCharacter = 'b';
aBlock(); // execute the block
// localCharacter now 'a'
Object and Block Variables (对象和block变量)
Blocks provide support for Objective-C and C++ objects, and other blocks, as variables.
block可以作为变量,支持Objective-C和C++对象,其他block。
Objective-C Objects(Objective&C 对象)
When a block is copied, it creates strong references to object variables used within the block. If you use a block within the implementation of a method:
If you access an instance variable by reference, a strong reference is made to self;
If you access an instance variable by value, a strong reference is made to the variable.
当一个block被拷贝了,他创建一个对block中使用的对象变量的强引用。如果你在块内实现了一个方法: 如果你通过引用访问一个实例变量,就对self建立一个强引用。如果你通过值访问了一个实例变量,就对变量建立一个强引用。
The following examples illustrate the two different situations:
下面的例子说明了不同的情况
dispatch_async(queue, ^{
// instanceVariable is used by reference, a strong reference is made to self
doSomethingWithObject(instanceVariable);
id localVariable = instanceV
dispatch_async(queue, ^{
localVariable is used by value, a strong reference is made to localVariable
(and not to self).
doSomethingWithObject(localVariable);
To override this behavior for a particular object variable, you can mark it with the __block storage type modifier.
通过重写一个特定对象变量,你可以使用_block存储类型修饰符来标记它。
C++ Objects (C++对象)
In general you can use C++ objects within a block. Within a member function, references to member variables and functions are via an implicitly imported this pointer and thus appear mutable. There are two considerations that apply if a block is copied:
If you have a __block storage class for what would have been a stack-based C++ object, then the usual copy constructor is used.
If you use any other C++ stack-based object from within a block, it must have a const copy constructor. The C++ object is then copied using that constructor.
通常情况下你可以在C++对象中使用block。在一个成员函数中,对成员变量和函数的引用通过一个隐晦的this指针实现,然后就出现了变数。如果一个block申请拷贝,这里有两种情况考虑: 如果你有一个基于栈的C++对象,里面有_block存储类,这个时候使用通常构造函数。如果你在一个block中使用其他的基于栈的C++对象,就必须使用常拷贝构造函数。C++对象拷贝的时候会用那个构造函数。
相关搜索:
相关阅读:
相关频道:
IOS教程最近更新iOS之Block代码块的定义及使用 - CSDN博客
iOS之Block代码块的定义及使用
不会使用Block的iOS程序员,不是一个合格的程序员
Block没有你想象中的那么难,不要害怕,不要畏惧,勇敢尝试
Block进阶:
Block其实就是一个代码块,把你想要执行的代码封装在这个代码块里,等到需要的时候再去调用。
个人觉得Block优势如下:第一可以使代码看起来更简单明了,第二可以取代以前的delegate使代码的逻辑看起来更清晰。
Block代码块和普通函数都是一段代码,两者有什么区别?
是这样描述的:
Block代码:是一个函数对象,是在程序运行过程中产生的;
普通函数:是一段固定代码,产生于编译期;
借一张图表达基本定义:
完整定义如下:
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
注1: Block的声明与赋值只是保存了一段代码段,必须调用才能执行内部代码
注2: ^被称作&脱字符&
void (^myBlock1)(void);
//无返回值,无参数
void (^myBlock2)(NSObject, int); //无返回值,有参数
NSString* (^myBlock3)(NSString* name, int age); //有返回值和参数,并且在参数类型后面加入了参数名(仅为可读性)
Block变量的声明
Block变量的声明格式为: 返回值类型(^Block名字)(参数列表);
注3:形参变量名称可以省略,只留有变量类型即可
// 声明一个无返回值,参数为两个字符串对象,叫做aBlock的Block
void(^aBlock)(NSString *x, NSString *y);
// 形参变量名称可以省略,只留有变量类型即可
void(^aBlock)(NSString *, NSString *);
巧记Block格式
很多人觉得Block格式定义很难记,其实我们可以通过与 java 函数方法对比加强记忆:
(String a, String b)
// Java的方法声明
(^MyBlockName) (String a, String b)
// iOS的block声明
block的定义
/*定义属性,block属性可以用strong修饰,也可以用copy修饰 */
@property (nonatomic, strong) void(^myBlock)();
//无参无返回值
@property (nonatomic, strong) void(^myBlock1)(NSString *);
//带参数无返回值
@property (nonatomic, strong) NSString *(^myBlock2)(NSString *);
//带参数与返回值
//定义变量
void(^myBlock)() =
//无参无返回值
void(^myBlock1)(NSString *) =
//带参数无返回值
NSString *(^myBlock2)(NSString *) =
//带参数与返回值
block被当做方法的参数
格式:(block类型)参数名称
- (void)test:(void(^)())testBlock
//无参无返回值
- (void)test1:(void(^)(NSString *))testBlock
//带参数无返回值
- (void)test2:(NSString *(^)(NSString *))testBlock
//带参数与返回值
使用typedef定义block
typedef void(^myBlock)();
//以后就可以使用myBlock定义无参无返回值的block
typedef void(^myBlock1)(NSString *);
//使用myBlock1定义参数类型为NSString的block
typedef NSString *(^myBlock2)(NSString *);
//使用myBlock2定义参数类型为NSString,返回值也为NSString的block
//定义属性
@property (nonatomic, strong) myBlock testB
//定义变量
myBlock testBlock =
//当做参数
- (void)test:(myBlock)testB
以下两者等价:
- (void) testAnimations:(void & & & (^) & &(void)&) &// 无参数
- (void) testAnimations:(void & & & (^) & &() & & & & & &) &// 无参数
block的赋值
格式:block = ^返回值类型 (参数列表) {函数主体}
注4:&通常情况下,可以省略返回值类型,因为编译器可以从存储代码块的变量中确定返回值的类型。
没有参数没有返回值
myBlock testBlock = ^void(){
NSLog(@&test&);
没有返回值,void可以省略
myBlock testBlock1 = ^(){
NSLog(@&test1&);
没有参数,小括号也可以省略
myBlock testBlock2 = ^{
NSLog(@&test2&);
有参数没有返回值
myBlock1 testBlock = ^void(NSString *str) {
NSLog(str);
myBlock1 testBlock = ^(NSString *str) {
NSLog(str);
有参数有返回值
myBlock2 testBlock = ^NSString *(NSString *str) {
NSLog(str)
return @&hi&;
有返回值时也可以省略返回值类型
myBlock2 testBlock2 = ^(NSString *str) {
NSLog(str)
return @&hi&;
声明Block变量的同时进行赋值
int(^myBlock)(int) = ^(int num){
return num * 7;
// 如果没有参数列表,在赋值时参数列表可以省略
void(^aVoidBlock)() = ^{
NSLog(@&I am a aVoidBlock&);
注5:如果没有参数,= 左边用括号表示,= 右边参数可以省略
Block变量的调用
// 调用后控制台输出&Li Lei love Han Meimei&
aBlock(@&Li Lei&,@&Han Meimei&);
// 调用后控制台输出&result = 63&
NSLog(@&result = %d&, myBlock(9));
// 调用后控制台输出&I am a aVoidBlock&
aVoidBlock();
Block作为OC函数参数
// 1.定义一个形参为Block的OC函数
- (void)useBlockForOC:(int(^)(int, int))aBlock
NSLog(@&result = %d&, aBlock(300,200));
// 2.声明并赋值定义一个Block变量
int(^addBlock)(int, int) = ^(int x, int y){
return x+y;
// 3.以Block作为函数参数,把Block像对象一样传递
[self useBlockForOC:addBlock];
// 将第2点和第3点合并一起,以内联定义的Block作为函数参数
[self useBlockForOC:^(int x, int y){
return x+y;
注意:当block作为方法参数时,定义(形参)和使用(实参)格式不一样。
& & // 定义block作为参数
& & + (RACSignal *)createSignal:(RACDisposable *&&(^)&(id&RACSubscriber&
subscriber)&&)didSubscribe {
& & & & return [RACDynamicSignal createSignal:didSubscribe];
& & // 使用block作为参数
& & RACSignal *signal =&
& & [RACSignal createSignal:^ &RACDisposable
*& &(&id&RACSubscriber& subscriber) {
& & & & [subscriber sendNext:letterSubject];
& & & & [subscriber sendNext:numberSubject];
& & & & [subscriber sendCompleted];
& & & & &// 如果不为空,返回类型是&RACDisposable *
Block在定义时并不会执行内部的代码,只有在调用时候才会执行。
使用示例:
// 在AAViewController.h定义
@property (nonatomic, copy) void (^successBlock)(NSInteger count);
// 在AAViewController.m赋值
if (self.successBlock && !_willUpdate){
self.successBlock([self.cards count]);
在BViewController.m中调用:
AAViewController *aa=[[[AAViewController alloc] init];
// 回调要如何处理
aa.successBlock=^(NSInteger count) {
if (count==0) {
// 处理代码
[aa httpRequest];
利用typedef为Block进行重命名
我们可以使用typedef为block进行一次重命名,方法跟为函数指针进行重命名是一样的:
typedef int (^Sum) (int,
int);这样我们就利用typedef定义了一个block,这个block的名字就是Sum,需要传入两个参数。当我们需要使用时,就可以这样做了:
Sum mysum = ^(int a, int b) {
return (a + b)*n;
};这样就完整的定义好了一个block了,接下来的使用如下:
#import &Foundation/Foundation.h&
typedef int (^Sum) (int, int);
int main(int argc, const char * argv[])
__block int n = 1;
@autoreleasepool {
Sum mysum = ^(int a, int b) {
return (a + b)*n;
NSLog(@&(3 + 5) * %i = %d&, n, mysum(3, 5));
Block在内存中的位置
根据Block在内存中的位置分为三种类型NSGlobalBlock,NSStackBlock, NSMallocBlock。
NSGlobalBlock:类似函数,位于text段;
NSStackBlock:位于栈内存,函数返回后Block将无效;
NSMallocBlock:位于堆内存。
BlkSum blk1 = ^ long (int a, int b) {
return a +
NSLog(@&blk1 = %@&, blk1);// 打印结果:blk1 = &__NSGlobalBlock__: 0x47d0&
int base = 100;
BlkSum blk2 = ^ long (int a, int b) {
return base + a +
NSLog(@&blk2 = %@&, blk2); // 打印结果:blk2 = &__NSStackBlock__: 0xbfffddf8&
BlkSum blk3 = [[blk2 copy] autorelease];
NSLog(@&blk3 = %@&, blk3); // 打印结果:blk3 = &__NSMallocBlock__: 0x902fda0&
blk1和blk2的区别在于:
blk1没有使用Block以外的任何外部变量,Block不需要建立局部变量值的快照,这使blk1与一般函数没有任何区别。
blk2与blk1唯一不同是的使用了局部变量base,
注意1:在定义(注意是“定义”,不是“运行”)blk2时,局部变量base当前值被copy到栈上,作为常量供Block使用。执行下面代码,结果是203,而不是204。
int base = 100;
base += 100;
BlkSum sum = ^ long (int a, int b) {
return base + a +
printf(&%ld&,sum(1,2));
在Block内变量base是只读的,如果想在Block内改变base的值,在定义base时要用 __block修饰:__block int base = 100;
__block int base = 100;
base += 100;
BlkSum sum = ^ long (int a, int b) {
base += 10;
return base + a +
printf(&%ld\n&,sum(1,2));
printf(&%d\n&,base);
输出将是214,211。
注意2:Block中使用__block修饰的变量时,将取变量此刻运行时的值,而不是定义时的快照。这个例子中,执行sum(1,2)时,base将取base++之后的值,也就是201,再执行Blockbase+=10; base+a+b,运行结果是214。执行完Block时,base已经变成211了。
局部自动变量:在Block中只读。Block定义时copy变量的值,在Block中作为常量使用,所以即使变量的值在Block外改变,也不影响他在Block中的值。
int base = 100;
BlkSum sum = ^ long (int a, int b) {
// base++; 编译错误,只读
return base + a +
printf(&%ld\n&,sum(1,2)); // 这里输出是103,而不是3
static变量、全局变量。如果把上个例子的base改成全局的、或static。Block就可以对他进行读写了。因为全局变量或静态变量在内存中的地址是固定的,Block在读取该变量值的时候是直接从其所在内存读出,获取到的是最新值,而不是在定义时copy的常量。
static修饰变量,效果与_ _block一样
static int base = 100;
BlkSum sum = ^ long (int a, int b) {
return base + a +
printf(&%d\n&, base);
printf(&%ld\n&,sum(1,2)); // 这里输出是3,而不是103
printf(&%d\n&, base);
输出结果是:
表明Block外部对base的更新会影响Block中的base的取值,同样Block对base的更新也会影响Block外部的base值。
Block变量,被__block修饰的变量称作Block变量。基本类型的Block变量等效于全局变量、或静态变量。
retain cycle
retain cycle问题的根源在于Block和obj可能会互相强引用,互相retain对方,这样就导致了retain cycle,最后这个Block和obj就变成了孤岛,谁也释放不了谁。比如:
@implementation TsetBlock
-(id)init{
if (self = [superinit]) {
self.testStr =@&中国&;
self.block = ^(NSString *name, NSString *str){
NSLog(@&arr:%@&,self.testStr); // 编译警告:Capturing 'self' strongly in this block is likely to lead to a retain cycle
网上大部分帖子都表述为&block里面引用了self导致循环引用&,其实这种说法是不严谨的,不一定要显式地出现&self&字眼才会引起循环引用。我们改一下代码,不通过属性self.testStr去访问String变量,而是通过实例变量_testStr去访问,如下:
@implementation TsetBlock
-(id)init{
if (self = [superinit]) {
self.testStr =@&中国&;
self.block = ^(NSString *name,NSString *str){
NSLog(@&arr:%@&, _testStr); // 同样出现: Capturing 'self' strongly in this block is likely to lead to a retain cycle
可以发现:
即使在你的block代码中没有显式地出现&self&,也会出现循环引用!只要你在block里用到了self所拥有的东西!
要分两种环境去解决:在ARC下不用__block ,而是用 __weak 为了避免出现循环引用
1.ARC:用__week
__weaktypeof (self) &weakSelf = 或者
__weak someClass *weakSelf =
2.MRC:用__block ,__block修饰的变量在Block
copy时是不会retain的,所以,也可以做到破解循环引用。
__block someClass *blockSelf =
使用如下代码解决循环引用
weakify(self);
success:^(AFHTTPRequestOperation *operation, id responseObject) {
& & strongify(self);
& & if (!self__weak_)
& & & &//...................
1、weakify(self); 创建一个指向self的弱引用
2、strongify(self); 当加上修饰符strong时,当别处把“self”释放掉,但调用该“self”的block如果仍然没有执行结束,那么系统就会等待block执行完成后再释放,对该“self”在block中的使用起到了保护作用。当block执行结束后会自动释放掉。
3、if (!self__weak_) 进行判断,如果在执行strongify(self)之前“self已经被释放掉了,则此时self=nil,所以直接return即可”
Block的copy、retain、release操作
对Block不管是retain、copy、release都不会改变引用计数retainCount,retainCount始终是1;
NSGlobalBlock:retain、copy、release操作都无效;
NSStackBlock:retain、release操作无效,必须注意的是,NSStackBlock在函数返回后,Block内存将被回收。即使retain也没用。容易犯的错误是[[mutableAarry addObject:stackBlock],在函数出栈后,从mutableAarry中取到的stackBlock已经被回收,变成了野指针。正确的做法是先将stackBlock copy到堆上,然后加入数组:[mutableAarry
addObject:[[stackBlock copy] autorelease]]。支持copy,copy之后生成新的NSMallocBlock类型对象。
NSMallocBlock支持retain、release,虽然retainCount始终是1,但内存管理器中仍然会增加、减少计数。copy之后不会生成新的对象,只是增加了一次引用,类似retain;
尽量不要对Block使用retain操作。
几个应用实例:
1、代码用在字符串数组排序
NSArray *stringArray = [NSArray arrayWithObjects:@&abc 1&, @&abc 21&, @&abc 12&,@&abc 13&,@&abc 05&,nil];
NSComparator sortBlock = ^(id string1, id string2)
return [string1 compare:string2];
NSArray *sortArray = [stringArray sortedArrayUsingComparator:sortBlock];
NSLog(@&sortArray:%@&, sortArray);
运行结果:sortArray:(&
&&&&abc 05&,
&&&&abc 1&,
&&&&abc 12&,
&&&&abc 13&,
&&&&abc 21&
2、代码块的递归调用
代码块想要递归调用,代码块变量必须是全局变量或者是静态变量,这样在程序启动的时候代码块变量就初始化了,可以递归调用
运行打印结果:
参考资料源自互联网
本文已收录于以下专栏:
相关文章推荐
事情从一般开发中一个massive viewController说起,一个巨大的vc一般少则上千行代码,多则上万行。
 这中情况下对代码的维护有致命性的障碍,个人亲身体验。
 当你试着从6000行...
关于Block,博主不才,简单分为三种来用:
像函数一样定义和使用,,不同于函数的是可以定义在方法内也可以定义在方法外
定义成property的属性来使用
用作修饰词
接下来一样样的来展示;
现在的无论是框架还是项目中,越来越多的使用block代码块。
个人觉得:第一可以使代码看起来更简单明了,第二可以取代以前的delegate使代码的逻辑看起来更清晰。
借一张图表达基本定义:
现在的无论是框架还是项目中,越来越多的使用block代码块。
个人觉得:第一可以使代码看起来更简单明了,第二可以取代以前的delegate使代码的逻辑看起来更清晰。
借一张图:
一 代码块的语法
 代码块的书写和C语言函数的语法格式有点像,例如下面求和函数分别用C 语言和代码块来实现:
int add(int a,int b)
  ...
推荐一篇深入研究block的blog:Block 深度研究
一.Block的基本使用
1.概念 
在iOS开发中Block的使用随处可见,使用场景也非常多,例如在网络中进行网络请求的回调...
最近看了松本行弘(日)编写的代码的未来这本书,其中有一个章节详细介绍了有关函数闭包的内容,后来联想到IOS中的代码块(Block),其实它就是对闭包的一种实现。
    
      那么...
一 代码块的语法
 代码块的书写和C语言函数的语法格式有点像,例如下面求和函数分别用C 语言和代码块来实现:
int add(int a,int b)
     return a+b;
原因是系统证书WWDR在日失效,需要更新WWDR系统证书
证书下载地址/certificationauthority/Apple...
翻译自 /blog//ios4-blocks-1
    iOS4引入了一个新特性,支持代码块的使用,这将从根本上改变你...
他的最新文章
讲师:王禹华
讲师:宋宝华
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)

我要回帖

更多关于 ios中block传值 的文章

 

随机推荐