关于Block块语法的那些事!
2015-12-12 19:57
489 查看
#import <Foundation/Foundation.h> #import "Sum.h" #import "Student.h" int global = 100 ; int main(int argc, const char * argv[]) { @autoreleasepool { NSLog(@"%i",sum(3,4)); int (*p) (int a ,int b ) = sum; // 调用函数指针 NSLog(@"%i",p(3,4)); // // Block 块语法,本质上是匿名函数 // block 的语法格式 /* 返回值类型(^block名)(参数列表) = ^(参数列表){ }; = 左边是block的声明,右边是block的实现 */ //无参无返block void (^myblock)(void) = ^(void){ NSLog(@"HAHSF"); }; myblock(); // 有参无返 void (^block1)(int ,int ) = ^( int a ,int b ){ NSLog(@"%d",a+b); }; block1(5,6); //有返无参 int (^block3)(void) = ^(void){ return 8; }; int i = block3(); NSLog(@"%i",i); // 有参有返 int (^block2) (int ,int ) = ^(int a ,int b){ return a+b; }; int c = block2(2 , 3); NSLog(@"%i",c); // block 的typedef typedef int (^ Block) (int ,int ); Block block4 = ^(int a ,int b){ return a*b; }; int mul = block4(5,6); NSLog(@"%i",mul); // 练习 { // 写一个 返回值为整型 参数为NSString(仅一个参 // 数)的block,实现将字符串转换为整型的功能。 int (^block)(NSString *) = ^(NSString *str){ return [str intValue]; }; int a = block(@"46851thsdf"); NSLog(@"%d",a); } // block 跟全局变量 void(^block11)(void) = ^(void){ global++; }; block11(); NSLog(@"%d",global); __block int a = 10 ; // 当代吗执行到block的定义时,并不会执行block的实现代码,但是会将block内部使用的局部变量拷贝一份,存入另一块内存空间,此时局部变量和Block内部使用的局部变量已经不是同一个,可以在定义局部变量的时候,加__block修饰,避免此问题 int (^block12)(int ) = ^(int b){ NSLog(@"%p",&a); a = 10 + a; return a+b; }; int l = block12(5); NSLog(@"%d",l); a= 30; int k = block12(5); NSLog(@"%d",k); // block跟数组排序 NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"s",@"g",@"c",@"a", nil]; [arr sortUsingSelector:@selector(compare:)]; [arr sortUsingComparator:^ NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { return -1 * [obj1 compare:obj2]; }]; NSLog(@"%@",arr); NSComparisonResult (^sortBlock) (NSString*,NSString*) = ^(NSString *str1,NSString *str2){ return [str1 compare:str2]; }; [arr sortUsingComparator:sortBlock]; NSLog(@"%@",arr); // 练习 { Student *stu1 = [Student studentWithName:@"lnhif" age:15 number:@"8"]; Student *stu2 = [Student studentWithName:@"gr" age:20 number:@"4"]; Student *stu3 = [Student studentWithName:@"rgh" age:16 number:@"7"]; NSMutableArray *mArr = [[NSMutableArray alloc]initWithObjects:stu1,stu2,stu3, nil]; typedef NSComparisonResult (^Block)(Student * ,Student *); Block blockArr = ^(Student *st1,Student *st2){ if (st1._age > st2._age) { return NSOrderedAscending; } else if(st1._age == st2._age) { return NSOrderedSame; }else { return NSOrderedDescending; } }; [mArr sortUsingComparator:blockArr]; NSLog(@"%@",mArr); NSComparisonResult (^blockArr1)(Student * ,Student *) = ^(Student *p,Student *q){ return -1 *[p._name compare:q._name]; }; [mArr sortUsingComparator:blockArr1]; NSLog(@"%@",mArr); NSComparisonResult (^blockArr2)(Student *,Student *) = ^(Student *p,Student *q){ return -1*[p._number compare:q._number]; }; [mArr sortUsingComparator:blockArr2]; NSLog(@"%@",mArr); } //字面量 //字符串 NSString *str = @"asjoefhuwbf"; //shuzu NSArray *NSarr = @[@"sgd",@"564561"]; //字典 NSDictionary *dic = @{@"key1":@"value1",@"key2":@"value2"}; // NSnumber NSNumber *num = @(3); } return 0; }
相关文章推荐
- YARN & Mesos,论集群资源管理所面临的挑战
- 基础练习 数列排序
- java多线程学习笔记——简单
- 《算法竞赛入门经典2ndEdition 》习题3-8 循环小数(Repeating Decimals, Uva202)
- 入门训练 A+B问题
- 期望值最大化算法
- 最短路径—Dijkstra算法和Floyd算法
- BZOJ 1951: [Sdoi2010]古代猪文( 数论 )
- Lua语言教程1 ——【变量输出,方法输出,if语句和for循环语句!】
- haproxy+keepalived搭建WEB群集
- poj2135 Farm Tour
- Windows 7 x64环境下JDK8安装过程
- Eclipse 中配置ehcache 的 xsd 文件
- √ ORDER BY x OFFSET 10000 LIMIT 4;
- VisualSVN Server提供程序无法执行所尝试的操作 0x80041024
- 【Win10开发】Toast通知
- JS触发按键事件
- C++异常机制的实现方式和开销分析
- 入门训练 序列求和
- Android Studio常用快捷键和一些高效编码的方法