Block、数组高级:Block语法、Block使用、Block实现数组排序

本文详细介绍了Block语法的概念及其在iOS开发中的应用。Block本质上是匿名函数,可以作为参数传递,并广泛应用于并行编程中。文章还提供了Block在排序、变量访问等方面的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Block:块语法,本质上是匿名函数(没有名称的函数)

标准C里面没有Block,C语法的后期扩展版本,加入了匿名函数。

C++、JS、Swift等语法,有类似语法,叫做闭包

Block语法和函数指针很相似

。函数指针:

           int(*p)(int x,int y) = sum;

in        函数指针类型int(*)(int x,int y) 即:指向两个整型参数,一个整型返回值函数的指针。

Block语法:   int (^Block)(int) +^(int num){   实现内容}   // 其中Block是变量,表示Block指向了整个函数实现。

Block类型:int(^)(int) 

Block变量:myBlock

Block 值:^int(int num){ return num};


block 是苹果最推崇的语法,iOS4.0 之后的类都添加了block支持。block通常当参数来使用。




 

//  main.m

#import<Foundation/Foundation.h>

#import "Student.h"

 

int sum(inta,int b){

return a + b;

}

 

int main(intargc, const char * argv[]) {

   @autoreleasepool {

 

 

       // int (*p)(inta,int b) = nil

       // p = sum;

       // 返回值类型:int类型

       // 标识符: *

       // 变量名: p

      //初值: nil

   //   函数指针类型:具有两个整型参数,一个整型返回值。int (int,int)

 

 

   //  block:本质是匿名函数.

 

       // 语法格式:返回值类型(^匿名函数名)(参数列表) = 初值

       //   int (^block)(int,int) = nil;

       //  返回值类型:int

       // 标识符: ^

       //  初值:nil

       // 匿名函数类型: 具有两个整形参数,一个整型返回值。

 

    // 函数实现

       //  因为匿名函数没有名字,因此我们用block存储了整个函数的实现

       //block = ^int(inta,int b){

       //  return  a + b;

       //};

 

       //   函数实现语法格式:

        //^返回值类型(参数列表){

            // 具体实现

        //}

 

         // 匿名函数实现的时候,返回值类型可以省略

       //  当参数为空的时候,可以省略小括号以及里面的内容

        //block = ^(inta,int b){

       //  return  a + b;

       //};

 

       // int (^block)(int,int) = ^int(inta,int b){

       //  return  a + b;

       //};

 

    //   匿名函数调用

   //   int sum = block(10,20);

     //  NSLog(@"%d",sum);

 

 

//       void (^block)(NSString *) = ^(NSString *str){

//            NSLog(@"%@",str);

//       };

//       

//       block(@"123");

 

//        // 无参数有返回值的匿名函数

//       NSString *(^block)() = ^{

//            NSString *str = @"321";

//            returnstr;

//       };

//       

//       NSString *p = block();

//       NSLog(@"%@",p);

//       

//       // 无参数无返回值的匿名函数

//       void (^block1)() = ^{

//            NSLog(@"232");

//       };

//       block1();

 

     // 为函数指针类型取别名

    // 1、将函数类型写出来

//        // 2、在最前面加上typedef,将变量名改成别名。

//       typedefint (*Fun)(int,int);

//   

//     // 为匿名函数取别名

//       typedefint (^myBlock)(int);

//       myBlock a = nil;

//

 

//       typedef void (^strBlock)(NSString *);

//       strBlock s = ^(NSString *str){

//            NSLog(@"%@",str);

//       };

//       s(@"123");

 

 

//       typedefNSString *(^cBlock)();

//       cBlock c = ^{

//                 NSString *str =@"321";

//                 returnstr;

//            };

//                NSString *p = c();

//                NSLog(@"%@",p);

 

 

 

//       typedefint (^Block)(NSString *);

//       

//       Block c = ^(NSString *str){

//            int m;

//            m = [strintValue];

//            return m;

//       };

//       int b = c(@"123");

//       NSLog(@"%d",b);

 

 

//         //  全局变量可以访问并且能够修改。

//        // 局部变量不允许在block内进行修改,

//        // 局部变量如果需要修改,定义局部变量的前面加上__block,或者加上static修饰。

//       // block 是为了并行编程做准备,即不允许变量被释放。

//       __blockint a = 100;

//        NSLog(@"111 %d",a);

//        typedef void (^Block)(int b);

//       Blockblock = ^(int b){

//            a = a + 100;

//            b = a + 100;

//             NSLog(@"222 %d",a);

//       };

//       NSLog(@"333 %d",a);

//       block(1);

//       NSLog(@"444 %d",a);

 

 

         //排序1

//       NSMutableArray *mArr =[NSMutableArrayarrayWithObjects:@"2",@"1",@"4",@"3",@"5",nil];

//       

//       

//       typedef void (^Block)(NSMutableArray *);

//       

//       Blockblock = ^(NSMutableArray *arr){

//            [arrsortUsingSelector:@selector(compare:)];

//       };

//      block(mArr);

//       NSLog(@"%@",mArr);

 

 

//          // 排序2

//       NSMutableArray *mArr =[NSMutableArrayarrayWithObjects:@"2",@"1",@"4",@"3",@"5",nil];

//       

//       // 系统写好的block 别名

//       NSComparator block = ^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {

//            return [obj1 compare:obj2];

//       };

//       [mArrsortUsingComparator:block];

//       NSLog(@"%@",mArr);

 

//       

//       Student *stu = [[Student alloc]initWithName:@"e" age:17number:234213];

//       Student *stu1 = [[Student alloc]initWithName:@"b" age:15number:534213];

//       Student *stu2 = [[Student alloc]initWithName:@"a" age:18number:434213];

//       Student *stu3 = [[Student alloc]initWithName:@"c" age:23number:134213];

//       Student *stu4 = [[Student alloc]initWithName:@"d" age:22number:334213];

//

//       NSMutableArray *students = [NSMutableArray arrayWithObjects:stu,stu1,stu2, stu3, stu4, nil];

//       

//       

//       // 姓名升序排列

//       NSComparator block = ^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {

//                    return [[obj1 name]compare:[obj2 name]];

//                    };

//        [studentssortUsingComparator:block];

//       //  obj1 和 obj2 指代sutdents中存储的对象

//        NSLog(@"%@",students);

//

//       

//       // 姓名降序排列

//       NSComparator block1 = ^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {

//            return -[[obj1 name] compare:[obj2name]];

//       };

//       [students sortUsingComparator:block1];

//       NSLog(@"%@",students);

//       

//         // 学号升序

//        NSComparator block2 = ^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {

//                    return [obj1 number] -[obj2 number];

//                };

//      [students sortUsingComparator:block2];

//        NSLog(@"%@",students);

//       

//       //学号降序

//       NSComparator block3 = ^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {

//            return -[obj1 number] +[obj2number];

//       };

//      [students sortUsingComparator:block3];

//       NSLog(@"%@",students);

 

 

 

 

       // 字面量:是一种新型的写法,字面量创建的对象是便利构造的,且是不可变的。

       // 字符串

NSString *s = @"123123";

       // 数组

NSArray *arr = @[@"123",@"hehe",@"haha"];

NSLog(@"%@",arr[0]);

       // 字典

NSDictionary *dic =@{@"key":@"value"};

NSLog(@"%@",[dicobjectForKey:@"key"]);

NSLog(@"%@",dic[@"key"]);

 

    }

return 0;

}

 

 

// *** 重写description ***

// %@时直接输出student的全部内容

- (NSString *)description{

return [NSStringstringWithFormat:@"%@%d %d",_name,_number,_age];

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值