`
lizhuang
  • 浏览: 889434 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Blocks表达式语法

 
阅读更多
简要介绍Block Literal Syntax,Block的出现是为了解决Objective C中的匿名函数问题。使用匿名函数可以使程序更为简洁,容易理解,减少程序员的函数命名工作。

1.Block语法

^ 返回值类型 参数列表 表达式
^int (int count){return count + 1;}



^ 参数列表 表达式
^(int count){ return count+ 1;}

返回值类型还是为int

^ 表达式
^void (void) { printf("Blocks\n";}

因为无返回值类型和参数列表所以可以省略为
^{printf("Blocks\n";}


2.Block类型变量

typedef int (^blk_t)(int);


typedef int(^SumBlockT)(int a,int b)


typedef void(^blockname)(NSDictionary* dict, NSError* error)


3.Block内赋值,__Block

__weak __typeof(self)weakSelf = self;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [weakSelf.delegate preOperateInBackgroundAtPageIndex:[self switchToPageIndexForIndex:index] ofBroadcastView:weakSelf];
        });

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics