设为首页 加入收藏

TOP

Object-C-代码块Block回顾(一)
2015-02-02 13:17:12 来源: 作者: 【 】 浏览:162
Tags:Object-C- 代码 Block 回顾
OC中的代码块是iOS4.0+ 和Mac OS X 10.6+ 引进的对C语言的扩展,用来实现匿名函数的特性。类似于其他语言脚本语言或者 编程语言中的闭包或者是Lambda表达式,可能第一眼看上去很怪异,不过开发的过程中会越来越大的越到Block,还是了解多一点比较好。Block方面的内容也有很多,本文从实际使用的角度大概讲一下Block的基本概念和实践。
?
首先看一个苹果官网的例子:
?
?
int (^Multiply)(int, int) = ^(int num1, int num2) {
? ? return num1 * num2;
};
int result = Multiply(7, 4); // Result is 28.
 上面的代码定义了一个Block,类似于C#中的委托,int表示返回类型,^是关键标示符,(int,int)是参数类型,不过需要注意的,正常的方式是将返回类型用()包裹,这里是将Block的名称包裹,网上有一个类似的图片,大家可以参考一下:
?
?
?
?
?
Block除了能够定义参数列表、返回类型外,还能够获取被定义时的词法范围内的状态(将变量作为参数传递之后,可以修改之后返回).Block都是一些简短代码片段的封装,适用作工作单元,通常用来做并发任务、遍历、以及回调。下面看一个通知的例子:
?
- (void)viewDidLoad {
? ?[super viewDidLoad];
? ? [[NSNotificationCenter defaultCenter] addObserver:self
? ? ? ? selector:@selector(keyboardWillShow:)
? ? ? ? name:UIKeyboardWillShowNotification object:nil];
}
??
- (void)keyboardWillShow:(NSNotification *)notification {
? ? // Notification-handling code goes here.
}
  以上是调用addServer方法的例子,不过也可以这么写:
?
?
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification
? ? ? ? ?object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
? ? ? ? ? ? ?// Notification-handling code goes here.
? ? }];
}
 苹果官网这个使用Block的例子如果第一次看可能不是那么好理解,如果仔细的看addServerName的定义发现:
?
?
- (id )addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block NS_AVAILABLE(10_6, 4_0);
其中Block参数定义的时候是这样的:
?
?
(void (^)(NSNotification *note))block NS_AVAILABLE(10_6, 4_0)
 括号包裹起来的内容,第一个是返回值类型,之后的用用括号包裹^,之后的 话才是传递类型,基于以上的理解,可以这么写:
?
?
void(^MyBlock)(NSNotification *)=^(NSNotification *note){
? ? ?note=nil;
?};
?[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?object:nil queue:[NSOperationQueue mainQueue] usingBlock:MyBlock];
 Block在苹果的API文档中随处看见其身影,常见的情况任务完成时回调处理,消息监听回调处理,错误回调处理,枚举回调,视图动画、变换,排序,比如说在NSDictionary中的方法中:
?
?
- (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(id key, id obj, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
任务完成时候的调用:
?
?
- (IBAction)animateView:(id)sender {
? ? CGRect cacheFrame = self.imageView.frame;
? ? [UIView animateWithDuration:1.5 animations:^{
? ? ? ? CGRect newFrame = self.imageView.frame;
? ? ? ? newFrame.origin.y = newFrame.origin.y + 150.0;
? ? ? ? self.imageView.frame = newFrame;
? ? ? ? self.imageView.alpha = 0.2;
? ? }
? ? ? ? ? ? ? ? ? ? ?completion:^ (BOOL finished) {
? ? ? ? ? ? ? ? ? ? ? ? ?if (finished) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// Revert image view to original.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self.imageView.frame = cacheFrame;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self.imageView.alpha = 1.0;
? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? }];
}
 通知机制中的处理:
?
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
? ? opQ = [[NSOperationQueue alloc] init];
? ? [[NSNotificationCenter defaultCenter] addObserverForName:@"CustomOperationCompleted"
? ? ? ? ? ? ?object:nil queue:opQ
? ? ? ? usingBlock:^(NSNotification *notif) {
? ? ? ? NSNumber *theNum = [notif.userInfo objectForKey:@"NumberOfIt
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言学习之Linux下TCP服务器与客.. 下一篇C中setjmp和longjmp用法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: