利用KVC的方式更方便地获取数组中对象的属性的最值平均值等
直接上代码 输出结果也在相应的代码里标注出来了
1 //main.m文件 2 #import <Foundation/Foundation.h> 3 #import "Student.h" 4 5 int main(int argc, const char * argv[]) { 6 @autoreleasepool { 7 8 NSMutableArray <Student *> *_studentArrM; 9 NSMutableArray <Student *> *_studentArrMTest; 10 11 _studentArrM = [NSMutableArray array]; 12 _studentArrMTest = [NSMutableArray array]; 13 14 Student *s1 = [Student studentWithNum:001 chinese:90.0 math:96.0 english:100.0]; 15 Student *s2 = [Student studentWithNum:002 chinese:90.0 math:100.0 english:96.0]; 16 Student *s3 = [Student studentWithNum:003 chinese:100.0 math:90.0 english:96.0]; 17 18 [_studentArrM addObject:s1]; 19 [_studentArrM addObject:s2]; 20 [_studentArrM addObject:s3]; 21 22 double mathAvg = [[_studentArrM valueForKeyPath:@"@avg.math"]doubleValue]; 23 double mathMax = [[_studentArrM valueForKeyPath:@"@max.math"]doubleValue]; 24 double mathMin = [[_studentArrM valueForKeyPath:@"@min.math"]doubleValue]; 25 double mathSum = [[_studentArrM valueForKeyPath:@"@sum.math"]doubleValue]; 26 NSLog(@"数学平均分%f 数学最高分%f 数学最低分%f 所有人的数学总分%f",mathAvg,mathMax,mathMin,mathSum); 27 28 /* 29 输出的内容为:数学平均分95.333333 数学最高分100.000000 数学最低分90.000000 所有人的数学总分286.000000 30 31 提示:有兴趣的话可以自己多测试几组数据 32 */ 33 34 //接下来试着处理一下数组中的对象的是否有重复的问题 35 //测试需要我们可以再次给可变数组添加一个重复的对象 36 [_studentArrM addObject:s2]; 37 [_studentArrM addObject:s2]; 38 39 NSArray *arrDistinct = [_studentArrM valueForKeyPath:@"@distinctUnionOfObjects.num"]; 40 NSArray *arrUnion = [_studentArrM valueForKeyPath:@"@unionOfObjects.num"]; 41 42 NSLog(@"DistinctArray %@ \n UnionArray %@",arrDistinct,arrUnion); 43 /* 44 输出的内容为: 45 DistinctArray ( 46 3, 47 2, 48 1 49 )//提示:没有重复的所有值 50 UnionArray ( 51 1, 52 2, 53 3, 54 2, 55 2 56 )//提示:有重复的所有值 57 */ 58 59 //为了处理 多个数组 中的重复值的情况再次添加一个一个对象到测试数组 60 [_studentArrMTest addObject:s2]; 61 62 NSLog(@"%@",[@[_studentArrM,_studentArrMTest] valueForKeyPath:@"@distinctUnionOfArrays.num"]); 63 NSLog(@"%@",[@[_studentArrM,_studentArrMTest] valueForKeyPath:@"@unionOfArrays.num"]); 64 /* 65 输出的内容为 66 (3, 67 2, 68 1 69 )//提示:没有重复的所有值 70 71 (1, 72 2, 73 3, 74 2, 75 2, 76 2 77 )//提示:有重复的所有值 78 79 */ 80 } 81 return 0; 82 }
//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文件 2 #import "Student.h" 3 4 @implementation Student 5 6 - (instancetype)initWithNum:(int)num chinese:(double)chinese math:(double)math english:(double)english{ 7 8 self = [super init]; 9 if(self){ 10 _num = num; 11 _chinese = chinese; 12 _math = math; 13 _english = english; 14 } 15 return self; 16 17 18 19 } 20 21 22 + (instancetype)studentWithNum:(int)num chinese:(double)chinese math:(double)math english:(double)english{ 23 24 return [[Student alloc]initWithNum:num chinese:chinese math:math english:english]; 25 26 } 27 28 @end
我会不定期分享 iOS 相关技术文章
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· Ollama系列05:Ollama API 使用指南
· 为什么AI教师难以实现
· 如何让低于1B参数的小型语言模型实现 100% 的准确率