• OC 06 Block、数组高级


    主要内容:

    ⼀、Block语法

    ⼆、Block使⽤

    三、Block实现数组排序 

    Block简介

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

    标准C⾥面没有Block,C语⾔言的后期扩展版本,加⼊了匿名函数。

    C++、JS、Swift等语⾔,有类似语法,叫做闭包。 Block语法和函数指针很相似。 

    回顾函数指针

    函数指针(变量):存放函数地址(函数名)的指针变量。 

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

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

    函数指针变量:p

    函数指针的值:sum 

    Block

    匿名函数:没有名称的函数。

    例如 int (int x, int y) 

    因为Block是匿名函数,block变量存放的函数的实现,通过block变量能直接调⽤用函数 

     没有名称的函数应该如何调⽤用?(有名称的话,可以直接调⽤,也可以通过函数指针来调⽤用)

    //1. ^ :脱字符
        //在OC 语言中, '^'表示Block块
        
        //声明一个block 变量
        int (^sumBlock) (int, int) = ^(int a, int b){
            return a+b;
        };
        NSLog(@"%d",sumBlock(10,30));
    1 int (^maxBlock)(int , int ) = ^(int a, int b){
    3         return a > b ? a:b;
    4     };
    5     int a = maxBlock(10,20);
    6     NSLog(@"%d",a);

    Block返回值类型:int (^maxBlock)(int a, int b)  a,b可以不写

    Block变量:a, b

    Block变量存储的值:是实现部分

    即: ^返回值类型 (参数列表)    函数体  

    Block 进行typedef

    typedef in (^BlockType)(int x, int y)

    原类型: int (^)(int x, int y)

    新类型: BlockType

    //给Block类型起一个别名
        typedef int (^MaxBlock) (int, int);
        MaxBlock sumBlock1 =^(int a, int b){
            return a + b;
        };
        NSLog(@"%d",sumBlock1(10,39));

    Block与局布变量的关系

    //Block 与局部变量的关系
        
       // int abc = 897;  //局部变量
        
       //__block修饰后 可以修改局部变量
       __block int count = 55;
        void (^sayHi) (int) = ^(int c){
            for (int i = 0; i < count; i++) {
                NSLog(@"");
            }
    //        abc = 90;  //可以使用局部变量,不可修改局部变量
           
        };
        sayHi(55);

    Block与局布变量的关系

    //全局变量
    int count = 200;
    int main(int argc, const char * argv[])
    {
        //Block 与全局变量
        void (^addNum)(void) = ^(void){       
            count++;
            NSLog(@"count = %d",count);
        };
        //调用
        addNum();
        //显示结果
        NSLog(@"count = %d",count);
    //练习1 用Block实现将字符串转为整形的功能
        int (^strint) (NSString *) = ^(NSString *string){
            return [string intValue];
        };
        int num = strint(@"890");
        NSLog(@"%d",num);

    Block数组排序

     //block数组排序
        NSArray *stringArray = [ NSArray arrayWithObjects:@"a18",@"19",@"90",@"88", nil];
        
        NSComparator sortBlock = ^(id string1, id string2){
            return [string1 compare:string2];
        };

       //将Block作为参数传递给方法

        NSArray *sortArray = [stringArray sortedArrayUsingComparator:sortBlock];
        NSLog(@"sortArray:%@",sortArray);
      //使用Block给动态数组进行排序 //不可变数组用法二需要在前面接收一下
     2     NSMutableArray *ageArray = [ NSMutableArray arrayWithObjects:@"113", @"89", @"99", nil];
     3     //法一
     4     NSComparisonResult (^sortBlock) (id, id) = ^(id obj1, id obj2){
     5         if ([obj1 intValue] > [obj2 intValue]) {
     6            return  NSOrderedDescending ;
     7         }else if([obj1 intValue] < [obj2 intValue]){
     8            return  NSOrderedAscending;
     9         }else{
    10             return NSOrderedSame;
    11         }
    12     };
    13     //将Block作为参数传递给方法
    14     [ageArray sortUsingComparator:sortBlock];
    15     NSLog(@"%@", ageArray);
    16     //法二 (使用这种方法)
    17    [ageArray sortUsingComparator:^NSComparisonResult(id obj3, id obj4) {
    18        if ([obj3 intValue] > [obj4 intValue]) {
    19            return NSOrderedAscending;
    20        }else{
    21            return NSOrderedSame;
    22        }
    23    }];

    1. 创建Person类
        实例变量: _name    _age
        方法:初始化方法
                   便利构造器
                   实例变量的赋值、取值方法
    2. 创建3个Person对象,放入数组中
    3. 在Person中添加compareByName:方法,使用此方法对数组进行排序,并输出
    4. 使用Block根据Person的age进行排序,并输出

    //根据age排序
        Person *person1 = [ Person personWith:@"da" age:19];
        Person *person2 = [Person personWith:@"ji" age:90];
        Person *person3 = [Person personWith:@"ko" age:2];
        NSMutableArray *array = [NSMutableArray arrayWithObjects:person1,person2,person3, nil];
        [array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            if ([obj1 getAge] > [obj2 getAge]) {
                return NSOrderedDescending;
            }else {
                return NSOrderedSame;
            }
        }];
        NSLog(@"%@",array);

     

    字面量

  • 相关阅读:
    太忙了
    Delphi 的接口(2) 第一个例子
    Delphi 的接口(3) 关于接口的释放
    VS.NET让我做了一场恶梦
    [推荐阅读]The Best Of .The NET 1.x Years
    向大家说声对不起
    [致歉]16:30~17:10电信网络出现问题
    服务器恢复正常
    [SharePoint]更改活动目录(AD)中用户名的问题
    [正式决定]博客园开始接受捐助
  • 原文地址:https://www.cnblogs.com/panny/p/4111020.html
Copyright © 2020-2023  润新知