• KVC取值的常用几种骚操作


    取纯数字数组中的最小值,最大值和平均值,求和

         NSArray<NSNumber*> *numbers = @[@(2.0),@(3.5),@(99.0),@(6.66),@(10.0)];
        
         NSArray<NSString*> *numberStrArr = @[@"2.0",@"3.5",@"99.0",@"6.66",@"10.0"];
    
        //最小值
        CGFloat minNumber = [[numbers valueForKeyPath:@"@min.doubleValue"] doubleValue];
        NSLog(@"minNumber: %.2f",minNumber);
        NSString *minStr = [[numberStrArr valueForKeyPath:@"@min.floatValue"] stringValue];
        NSLog(@"minStr: %@",minStr);
    
        //最大值
        NSInteger maxNumber = [[numbers valueForKeyPath:@"@max.integerValue"] integerValue];
        NSLog(@"maxNumber: %ld",maxNumber);
        NSString *maxNumberStr = [[numbers valueForKeyPath:@"@max.integerValue"] stringValue];
        NSLog(@"maxNumberStr: %@",maxNumberStr);
    
         //平均值
        NSString *avgNumber = [[numbers valueForKeyPath:@"@avg.doubleValue"] stringValue];
        NSLog(@"avgNumber: %@",avgNumber);
        NSString *avgNumberStr = [[numbers valueForKeyPath:@"@avg.doubleValue"] stringValue];
        NSLog(@"avgNumberStr: %@",avgNumberStr);
    
        //求和
        CGFloat sum = [[numbers valueForKeyPath:@"@sum.floatValue"] floatValue];
        //NSLog(@"sum: %.2f",sum);
    
        NSArray<NSNumber*> *numbers001 = @[@(2.0),@(2.0),@(99.0),@(6.66),@(6.66)];
        //去重生成新的数组
        NSArray<NSNumber*> *newNumbers = [numbers001 valueForKeyPath:@"@distinctUnionOfObjects.floatValue"];
       // NSLog(@"newNumbers: %@",newNumbers);
    
        NSArray *numbers002 = @[@(2.22),@(2.22),@(99.99),@(6.66),@(6.66)];
        //与distinctUnionOfObjects去重相反 生成指定条件全新的数组
        NSArray *newNumbers = [numbers002 valueForKeyPath:@"@unionOfObjects.stringValue"];
        //NSLog(@"newNumbers: %@",newNumbers);
    
    //控制台打印如下:     
        minNumber: 2.00
        minStr: 2
       
        maxNumber: 99
        maxNumberStr: 99
        
        avgNumber: 24.232
        avgNumberStr: 24.232
    
    

    数组内部元素类型转换

        NSArray<NSNumber*> *numbers = @[@(2.0),@(3.5),@(99.0),@(6.66),@(10.0)];
          
        //[NSNumber]数组转为[NSString]数组
        NSArray<NSString*> *stringArray = [numbers valueForKeyPath:@"intValue.stringValue"];
        NSLog(@"stringArray: %@",stringArray);
    
        //字符串数组 转为 字符串数组长度的数组
        NSArray<NSNumber*> *lengthArr = [stringArray valueForKeyPath:@"length"];
        NSLog(@"lengthArray: %@",lengthArr);
    
    //控制台输出如下: 
    stringArray: (
                  2,
                  3,
                  99,
                  6,
                  10
                  )
    
    lengthArray: (
                  1,
                  1,
                  2,
                  1,
                  2
                  )
    
    
    

    数组每个子元素调用指定方法

        NSArray<NSString *> *nameArr = @[@"CoderWGB",@"Apple",@"iOS",@"Android",@"Flutter"];
        //调用系统方法
        NSLog(@"全部转小写: %@",[nameArr valueForKeyPath:@"lowercaseString"]);
        NSLog(@"全部转大写: %@",[nameArr valueForKeyPath:@"uppercaseString"]);
        NSLog(@"全部首字符大写: %@",[nameArr valueForKeyPath:@"capitalizedString"]);
        // 调用自定义方法
        NSLog(@"全部元素之前插入  字符: %@",[nameArr valueForKeyPath:@"formatApplePrefix"]);
    
    //写一个自定义方法
    @interface NSString (Extension)
    - (NSString *)formatApplePrefix;
    @end
    
    @implementation  NSString (Extension)
    - (NSString *)formatApplePrefix{
        return [NSString stringWithFormat:@"  %@",self];
    }
    @end
    
    //控制台打印如下: 
    全部转小写: (
        coderwgb,
        apple,
        ios,
        android,
        flutter
    )
     全部转大写: (
        CODERWGB,
        APPLE,
        IOS,
        ANDROID,
        FLUTTER
    )
    全部首字符大写: (
        Coderwgb,
        Apple,
        Ios,
        Android,
        Flutter
    )
    全部元素之前插入  字符: (
        " Uf8ff CoderWGB",
        " Uf8ff Apple",
        " Uf8ff iOS",
        " Uf8ff Android",
        " Uf8ff Flutter"
    )
    

    比起自己写循环遍历去拼接字符串什么的,效果还是挺明显的,并且调用代码相对解耦,可以把业务逻辑分离出去

  • 相关阅读:
    步步为营-15-文件夹的操作
    步步为营-14-文件操作
    步步为营-13-日期转化
    步步为营-12-Dictionary-翻译
    步步为营-11-List<T>泛型的简单练习
    步步为营-10-string的简单操作
    步步为营-09-简单工厂类-计算器
    B. Fixed Points
    C. Cd and pwd commands
    Queries on a String
  • 原文地址:https://www.cnblogs.com/wgb1234/p/12486451.html
Copyright © 2020-2023  润新知