利用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 相关技术文章