OC - 第六章 Block

来源:互联网 发布:蚁群算法c语言程序 编辑:程序博客网 时间:2024/06/06 07:44
//                      第六章 Block        //函数        int sum(int a, int b);        int sum(int a, int b){            return a + b;        }        //函数指针        int (*p)(int a, int b) = sum; //     block 块语法, 本质上是匿名函数 可以在函数里面定义block//       一、block 的语法格式        /*            返回值类型(^block名)(参数列表) = ^(参数列表){函数体}            = 左边是block的声明, 右边是函数实现        *///         block定义部分的参数名和函数指针一样能为空, 但是声明部分参数名必须与函数体内部一致//      二、block的分类:        {            //1)无参无返            void(^block1)(void) = ^(void){                NSLog(@"无参无返");            };//注意分号            block1();            //2)有参无返            void(^block2)(int a) = ^(int b){                NSLog(@"%d是有参无返的参数",b);            };            block2(3);            //3)有返无参            int(^block3)(void) = ^(void){                return 3;            };            NSLog(@"有返无参的返回值是%d",block3());            //4)有参有返            int(^block4)(int a, int b) = ^(int a, int c){                return a + c;            };            NSLog(@"有参有返的返回值是%d",block4(3, 4));        }//      三、block的typedef:        typedef int(^block)(int , int);        block block5 = ^(int a, int b){            return a + b;        };        //函数指针的typedef: typedef int(*p)(int , int)//        练习: 将 数字 字符串转成 int 类型        {            int(^block)(NSString *) = ^(NSString *str){                return [str intValue];            };            NSString *str = @"2354";            NSLog(@"%d",block(str));        }//        四、block跟全局变量        {            void(^block)(void) = ^(void){                globle ++;            };            block();//调用block才会执行代码            NSLog(@"全局变量%d",globle);//全局变量++        }//        五、block跟局部变量        {            __block int a = 10;            NSLog(@"%p",&a);            //当代码执行到block的定义时, 并不会执行block的实际代码, 但是会将 block内部使用得到的局部变量拷贝一份, 存入另一块内存空间, 此时 局部变量和block内部使用的局部变量已经不是同一个了            //在定义局部变量前面加修饰 _block, 避免此问题            char *p = "sdk";            NSLog(@"常量区:%p", &p);            int (^block)(int b) = ^(int b){                a ++;                NSLog(@"a堆区: %p", &a);                return b + a;            };            NSLog(@"%d", block(3));            a = 30;            NSLog(@"a%p",&a);            NSLog(@"%d", block(3));        }//        六、block实现数组排列        {            //数组方法排序            NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"g", @"o", @"o", @"d", nil];            [arr sortUsingSelector:@selector(compare:)];            NSLog(@"%@", arr);            //block排序            [arr sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {                return [obj1 compare:obj2];            }];            NSLog(@"%@", arr);            //block另外一种方法            NSComparisonResult(^block)(NSString *, NSString *) = ^(NSString *str1, NSString *str2){                return [str1 compare:str2];            };//            练习: 按学号升序            {                [studentArr sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {//                    Student *stu1 = (Student *)obj1;//                    Student *stu2 = (Student *)obj2;                    if ([obj1 age] < [obj2 age]) {                        return NSOrderedAscending;                    }                    else if([obj1 age] > [obj2 age]){                        return NSOrderedDescending;                    }                    else{                        return NSOrderedSame;                    }                }];            }            //字面量            {                //字符串                NSString *str = @"iPhone";                //数组                NSArray *str1 = @[@"i", @"p", @"h"];                arr[1];                //字典                NSDictionary *dic = @{@"key":@"value"};                dic[@"key"];                //NSNumber                NSNumber *num = @(3);            }        }
0 0
原创粉丝点击