優雅的獲取數組的最大值

如何優雅的獲取數組的最大值,其實oc可以通過valueForKeyPath方法來設置你想要獲取的一些數組,比如數組的最大值,最小值,平均值等。下面是實踐:

NSMutableArray* array = [NSMutableArray array];
for (int i = 0; i < 10; i++) {
    CGFloat num = arc4random() % 100 + 1;
    [array addObject:[NSNumber numberWithFloat:num]];
}
CGFloat maxValue = [[array valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat minValue = [[array valueForKeyPath:@"@min.floatValue"] floatValue];

重點在這句話上 !

@”@max.floatValue”    (獲取最大值)
@”@min.floatValue”    (獲取最小值)
@”@avg.floatValue”      (獲取平均值)
@”@count.floatValue”   (獲取數組大小) 
...
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容