直接上代码 输出结果也在相应的代码里标注出来了

 //main.m文件
#import <Foundation/Foundation.h>
#import "Student.h" int main(int argc, const char * argv[]) {
@autoreleasepool { NSMutableArray <Student *> *_studentArrM;
NSMutableArray <Student *> *_studentArrMTest; _studentArrM = [NSMutableArray array];
_studentArrMTest = [NSMutableArray array]; Student *s1 = [Student studentWithNum: chinese:90.0 math:96.0 english:100.0];
Student *s2 = [Student studentWithNum: chinese:90.0 math:100.0 english:96.0];
Student *s3 = [Student studentWithNum: chinese:100.0 math:90.0 english:96.0]; [_studentArrM addObject:s1];
[_studentArrM addObject:s2];
[_studentArrM addObject:s3]; double mathAvg = [[_studentArrM valueForKeyPath:@"@avg.math"]doubleValue];
double mathMax = [[_studentArrM valueForKeyPath:@"@max.math"]doubleValue];
double mathMin = [[_studentArrM valueForKeyPath:@"@min.math"]doubleValue];
double mathSum = [[_studentArrM valueForKeyPath:@"@sum.math"]doubleValue];
NSLog(@"数学平均分%f 数学最高分%f 数学最低分%f 所有人的数学总分%f",mathAvg,mathMax,mathMin,mathSum); /*
输出的内容为:数学平均分95.333333 数学最高分100.000000 数学最低分90.000000 所有人的数学总分286.000000 提示:有兴趣的话可以自己多测试几组数据
*/ //接下来试着处理一下数组中的对象的是否有重复的问题
//测试需要我们可以再次给可变数组添加一个重复的对象
[_studentArrM addObject:s2];
[_studentArrM addObject:s2]; NSArray *arrDistinct = [_studentArrM valueForKeyPath:@"@distinctUnionOfObjects.num"];
NSArray *arrUnion = [_studentArrM valueForKeyPath:@"@unionOfObjects.num"]; NSLog(@"DistinctArray %@ \n UnionArray %@",arrDistinct,arrUnion);
/*
输出的内容为:
DistinctArray (
3,
2,
1
)//提示:没有重复的所有值
UnionArray (
1,
2,
3,
2,
2
)//提示:有重复的所有值
*/ //为了处理 多个数组 中的重复值的情况再次添加一个一个对象到测试数组
[_studentArrMTest addObject:s2]; NSLog(@"%@",[@[_studentArrM,_studentArrMTest] valueForKeyPath:@"@distinctUnionOfArrays.num"]);
NSLog(@"%@",[@[_studentArrM,_studentArrMTest] valueForKeyPath:@"@unionOfArrays.num"]);
/*
输出的内容为
(3,
2,
1
)//提示:没有重复的所有值 (1,
2,
3,
2,
2,
2
)//提示:有重复的所有值 */
}
return ;
}
//Student.h文件
#import <Foundation/Foundation.h> @interface Student : NSObject /**
学号
*/
@property (nonatomic,assign)int num;
/**
语文成绩
*/
@property (nonatomic,assign)double chinese;
/**
语文成绩
*/
@property (nonatomic,assign)double math;
/**
语文成绩
*/
@property (nonatomic,assign)double english; - (instancetype)initWithNum:(int)num chinese:(double)chinese math:(double)math english:(double)english; + (instancetype)studentWithNum:(int)num chinese:(double)chinese math:(double)math english:(double)english;
 1 //Student.m文件
#import "Student.h" @implementation Student - (instancetype)initWithNum:(int)num chinese:(double)chinese math:(double)math english:(double)english{ self = [super init];
if(self){
_num = num;
_chinese = chinese;
_math = math;
_english = english;
}
return self; } + (instancetype)studentWithNum:(int)num chinese:(double)chinese math:(double)math english:(double)english{ return [[Student alloc]initWithNum:num chinese:chinese math:math english:english]; } @end

利用KVC的方式更方便地获取数组中对象的属性的最值平均值等的更多相关文章

  1. freemarker获取封装类中对象的属性

    freemarker获取封装类中对象的属性 1.设计思路 (1)封装学生类 (2)创建数据模型 (3)新建student.ftl (4)运行Junit测试文件,生成HTML文件 2.封装学生类 Stu ...

  2. freemarker获取封装类中对象的属性(六)

    freemarker获取封装类中对象的属性 1.设计思路 (1)封装学生类 (2)创建数据模型 (3)新建student.ftl (4)运行Junit测试文件,生成HTML文件 2.封装学生类 Stu ...

  3. PHP获取数组最后一个元素的键和值

    <?php /** * PHP获取数组中最后一个元素下标和值 */ $arr = ['1' => 'name', '3' => 2, 5 => 6, 'name' => ...

  4. php 获取数组第一个key 第一个键值对 等等

    PHP 获取数组中的第一个元素或最后一个元素的值或者键值可以使用 PHP 自带的数组函数. PHP 获取数组中的第一个元素的值或者键值所使用的函数: current() - 返回数组中当前元素值(即: ...

  5. springboot中spring.profiles.active来引入多个properties文件 & Springboot获取容器中对象

    1.    引入多个properties文件 很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据, ...

  6. js获取数组中的最大值/最小值

    目录 前言 1. 使用Math的静态方法max/min 1.1 结合ES6的扩展运算符...使用 1.2 结合apply/call方法来使用 1.3 结合reduce来使用 2. 排序获取 2.1 只 ...

  7. php获取数组中重复数据的两种方法

    分享下php获取数组中重复数据的两种方法. 1,利用php提供的函数,array_unique和array_diff_assoc来实现 <?php function FetchRepeatMem ...

  8. Javascript获取数组中的最大值和最小值的方法汇总

    比较数组中数值的大小是比较常见的操作,下面同本文给大家分享四种放哪广发获取数组中最大值和最小值,对此感兴趣的朋友一起学习吧   比较数组中数值的大小是比较常见的操作,比较大小的方法有多种,比如可以使用 ...

  9. Java获取未知类型对象的属性

    获取未知类型对象的属性通常有两种方式: 一是通过自定义注解的方式,通过获取被注解的属性从而获取属性的值,这种方式也是Spring参数注入的重要实现手段 二是通过反射获取属性的名称,通过属性名从而获取属 ...

随机推荐

  1. Codeforces Round #418 (Div. 2).C two points

    C. An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 m ...

  2. mac下,mysql5.7.18连接出错,错误信息为:Access denied for user 'root'@'localhost' (using password: YES)

    mac下,mysql5.7.18连接出错,错误信息为:Access denied for user 'root'@'localhost' (using password: YES)()里面的为shel ...

  3. 使用random输出10个double型的随机数

    使用random输出10个double型的随机数 代码如下: package Day05;import java.util.Random;import java.util.Scanner; publi ...

  4. Android Studio2.1 Run APP:Error: Execution failed for task

    Android Studio2.1 Run APP时,遇到错误 Error: Execution failed for task ':app:clean'. Unable to delete file ...

  5. 2017年1月1日 App Store中的所有应用都必须启用 App Transport Security安全功能

    2017年1月1日 App Store中的所有应用都必须启用 App Transport Security安全功能,否则极有可能被拒! 在WWDC 2016开发者大会上,苹果宣布了一个最后期限:到20 ...

  6. tensorflow bias_add应用

    import tensorflow as tf a=tf.constant([[1,1],[2,2],[3,3]],dtype=tf.float32) b=tf.constant([1,-1],dty ...

  7. 【Log4j】分包,分等级记录日志信息

    在开发中我们经常会将不同包下的日志信息在不同的地方输出,以便于以后出问题能够直接在对应的文件中找到对应的信息! 例如:在spring+SpringMVC+mybatis的框架中,我们经常会将sprin ...

  8. WPF转换器之通用转换器

    WPF中的转换器是一个非常好的数据类型转换解决方案,实用和强大, 它的作用是将源数据转换为WPF自身需要的类型,对数据实体没有侵略性,会在项目工程中频繁使用.所以掌握转换器是WPF开发的必备技能. 我 ...

  9. Java之集合初探(二)Iterator(迭代器),collections,打包/解包(装箱拆箱),泛型(Generic),comparable接口

    Iterator(迭代器) 所有实现了Collection接口的容器都有一个iterator方法, 用来返回一个实现了Iterator接口的对象 Iterator对象称作迭代器, 用来方便的实现对容器 ...

  10. 基于Python使用SVM识别简单的字符验证码的完整代码开源分享

    关键字:Python,SVM,字符验证码,机器学习,验证码识别 1   概述 基于Python使用SVM识别简单的验证字符串的完整代码开源分享. 因为目前有了更厉害的新技术来解决这类问题了,但是本文作 ...