从NSArray,KVC获取最大日期 [英] Getting Max Date from NSArray, KVC

查看:112
本文介绍了从NSArray,KVC获取最大日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSDates数组,我想从数组中获取最大的NSDate。虽然我可以总是排序他们,抓住第一/最后,有没有办法做到这一点与KeyValueCoding或一些其他快速一种衬垫的方式?我知道,如果对象有一个date属性,我可以使用类似valueForKeyPath @@ max.date的东西,但如果对象是日期本身,如果

I've got an array of NSDates and I'd like to grab the largest NSDate from the array. While i could always sort them and grab the first/last, is there a way to do this with KeyValueCoding or some other quick one liner kind of way? I know that I could use something like valueForKeyPath@"@max.date" if the objects had a date property, but what if the objects are dates themselves??

推荐答案

您可以使用

NSDate *maxDate = [dateArray valueForKeyPath:@"@max.self"];

这将给你从数组最大的日期。您不必在执行此操作之前对数组进行排序。

This will give you the largest date from array. You dont have to sort the array before doing this.

文档


@max操作符比较属性由操作符右侧的
键路径指定,并返回找到的最大值
。最大值是使用
的compare:方法在指定键路径上的对象确定的。被比较的属性对象
必须支持彼此的比较。如果键路径右侧的
侧的值为nil,则会被忽略。

The @max operator compares the values of the property specified by the key path to the right of the operator and returns the maximum value found. The maximum value is determined using the compare: method of the objects at the specified key path. The compared property objects must support comparison with each other. If the value of the right side of the key path is nil, it is ignored.

do compare:,然后找出最大值。

Note that @max will do compare: and then will find out the max value.

这篇关于从NSArray,KVC获取最大日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆