Objective-C Block详解
2016-01-21 15:05
302 查看
Block在iOS开发中经常常用到,例如传值和回调等场景都能利用block来实现,这里简单介绍一下Block的使用和注意事项。
同C语言中的函数指针和swift中的闭包非常相似,block的使用也非常灵活,可以带参数、可以传参数、可以有返回值,也可以当做变量。Block本质上是一组代码块,可以在你想要的时间和想要的地方执行代码块中的代码。
下面根据函数的形式还把Block分成四类来简单介绍一下:
同时,Block也可以这样声明:
通过一个demo来掩饰block传值:
建立两个UIViewController(FirstViewController,SecondViewController),为展示block特性,demo使用mrc模式。
FirstViewController.m中代码:
SecondViewController.m中代码:
效果:
2 回调
这里介绍一中最常用的回调场景,进行某项操作成功或者失败的回调。需要将Block当做参数参入方法中。
首先tpedef一个Block:
方法实现:
调用该方法的时候根据bsuccess和error_code两个参数来进行相关操作即可,如改变UI或者提示操作失败。
同C语言中的函数指针和swift中的闭包非常相似,block的使用也非常灵活,可以带参数、可以传参数、可以有返回值,也可以当做变量。Block本质上是一组代码块,可以在你想要的时间和想要的地方执行代码块中的代码。
一 基本语法:
用^操作符来声明一个Block变量,它表示一个Block的开始。下面根据函数的形式还把Block分成四类来简单介绍一下:
//无参无返 void (^block1)() = ^(){ NSLog(@"这是一个无参无返回值的block"); }; block1();//执行上面的代码块 //无参有返 NSInteger (^block2)() = ^(){ return NSIntegerMax; }; NSLog(@"%ld",block2()); NSString * (^block3)() = ^(){ return @"能"; }; NSLog(@"%@", block3()); //有参无返 void (^block4)(NSArray *) = ^(NSArray *array){ NSLog(@"%@", array); }; NSArray *arr = @[@"a", @"b", @"c"]; block4(arr); //有参有返 //求两个数的和 NSInteger (^block6)(NSInteger, NSInteger) = ^(NSInteger num1, NSInteger num2){ return num1 + num2; }; NSInteger sum = block6(2, 3); NSLog(@"%ld", sum);
同时,Block也可以这样声明:
/返回值为NSInteger,参数为两个NSInteger数据 typedef NSInteger (^CustomBLOCK) (NSInteger, NSInteger); //定义一个求和block CustomBLOCK sumBlock = ^(NSInteger num1, NSInteger num2){ return num1 + num2; }; NSLog(@"%ld", sumBlock(7, 8)); //Block可以使用局部变量,但是不可以修改局部变量的值.需要在前面加上__block来修饰 //Block内部可以访问和修改全局变量 __block NSInteger times = 3; CustomBLOCK timesBlock = ^(NSInteger num1, NSInteger num2){ times ++; return (num1 + num2) * times; }; NSLog(@"%ld", timesBlock(1, 2)); //把字符串反向输出 NSString * (^block5)(NSString *) = ^(NSString *string){ NSMutableString * resultStr = [[NSMutableString alloc]init]; for (NSInteger i = string.length - 1; i >= 0; i--) { NSString *tempStr = [string substringWithRange:NSMakeRange(i , 1)]; [resultStr appendString:tempStr]; } return resultStr; }; NSLog(@"%@", block5(@"abcd"));
二 应用场景
平时我们使用最多的是属性传值,但是属性传值只局限于从前到后,从后往前传值的话由于是出栈或者dismiss操作,页面会被销毁,导致页面内的属性也被销毁。这是我们可以利用代理,通知和Block三种方法来实现传值操作,这里介绍一下block传值。通过一个demo来掩饰block传值:
建立两个UIViewController(FirstViewController,SecondViewController),为展示block特性,demo使用mrc模式。
FirstViewController.m中代码:
#import "FirstViewController.h" #import "SecondViewController.h" @interface FirstViewController () @property (nonatomic, retain)UITextField *textField; @end @implementation FirstViewController - (void)dealloc { [_textField release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; self.textField = [[[UITextField alloc]initWithFrame:CGRectMake(150, 200, 100, 100)]autorelease]; self.textField.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.textField]; self.view.backgroundColor = [UIColor redColor]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ SecondViewController *secondVC = [[SecondViewController new]autorelease]; secondVC.string = self.textField.text; //展开block secondVC.block = ^(NSString *string){ self.textField.text = string; }; [self.navigationController pushViewController:secondVC animated:YES]; } SecondViewController.h中代码: #import <UIKit/UIKit.h> typedef void(^BLOCK) (NSString *); @interface SecondViewController : UIViewController //block作为属性,一定要用copy语句 //将block从栈区拷贝到堆区 @property (nonatomic, copy)BLOCK block; @property (nonatomic, copy)NSString *string; @end
SecondViewController.m中代码:
#import "SecondViewController.h" @interface SecondViewController () @property (nonatomic, retain)UITextField *textField; @end @implementation SecondViewController - (void)dealloc { NSLog(@"正是一个dealloc方法"); [_textField release]; //block释放的时候要使用BlocK_release Block_release(_block); [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; //隐藏返回按钮 self.navigationItem.hidesBackButton = YES; self.textField = [[[UITextField alloc]initWithFrame:CGRectMake(150, 200, 100, 100)]autorelease]; self.textField.backgroundColor = [UIColor whiteColor]; self.textField.text = self.string; [self.view addSubview:self.textField]; self.view.backgroundColor = [UIColor brownColor]; } //返回第一页面 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //为了避免循环引用,我们使用__block来修饰(ARC下为__weak),Block中不要出现self,否则会形成循环引用,导致block不会被释放掉 // __block typeof (self) weakSelf = self; // // self.block = ^(NSString *str){ // // [weakSelf test];//这里为了演示使用Block时注意循环引用的问题,不要再这里展开Block; // }; //在返回第一个页面的时候,执行block self.block(self.textField.text); [self.navigationController popViewControllerAnimated:YES]; } //- (void)test{ // NSLog(@"这是一个测试方法"); //}
效果:
2 回调
这里介绍一中最常用的回调场景,进行某项操作成功或者失败的回调。需要将Block当做参数参入方法中。
首先tpedef一个Block:
typedef void(^ResultBlock)(BOOL bsuccess,int error_code);
方法实现:
-(id) funWithBlock:(ResultBlock)block{ if () { block(YES,0); } else if (){ block(YES,-1); } else{ block(YES,0); } }
调用该方法的时候根据bsuccess和error_code两个参数来进行相关操作即可,如改变UI或者提示操作失败。
三 注意事项:
在上面的代码中Block的使用注意事项都已经标出来了,小伙伴们仔细看看吧~相关文章推荐
- Objective-C MD5加密方法
- Objective-C多线程详解(NSThread、NSOperation、GCD)
- Android学习笔记——用ObjectAnimator平移动画仿微信表情雨的简单实现
- 混编Objective-C 和 Swift
- Objective-C系统原生图片下载方法
- Objective-C的Runtime机制的应用示例总结
- 2.7 The Object Model -- Bindings, Observers, Computed Properties:What do I use when?
- 2.6 The Object Model -- Bindings
- JSONObject put,accumulate,element的区别
- 使用Xcode HeaderDoc和Doxygen文档化你的Objective-C和Swift代码
- Return objects to enable chaining of functions
- 2.5 The Object Model -- Observers
- cannot open shared object file: No such file or directory如何解决
- python 中getattr(object,name,default),setattr(object,name,default),hasattr(),delattr()
- object c 对象和普通的c对象之间的转化
- 2.4 The Object Model -- Computed Properties and Aggregate Data with @each(计算的属性和使用@each聚合数据)
- os - Parse Issues in NSObjCRuntime, NSZone, and NSObject
- JSONObject、JSONArray(转载)
- 用Myeclipse运行项目报getClassInstance Line: 209 - com/opensymphony/xwork2/spring/SpringObject错
- 2.3 The Object Model -- Computed Properties