简体   繁体   English

如何从NSArray获取具有最低属性值的对象

[英]How to get the object with the lowest property value from an NSArray

I have an array of CLLocation objects. 我有一个CLLocation对象数组。 If I use 如果我用

CLLocation *bestLocation = [locations valueForKeyPath:@"@min.horizontalAccuracy"];

I'm getting the lowest horizontalAccuracy value. 我得到最低的horizo​​ntalAccuracy值。 But I want the OBJECT that contains the lowest value, so that I can later get it's proper coodinates. 但是我想要包含最低值的对象,以便以后可以获取适当的余弦。 How can I do that? 我怎样才能做到这一点?

I also tried with 我也尝试过

CLLocation *bestLocation = [locations valueForKeyPath:@"@min.self.horizontalAccuracy"];

but got the same results. 但是得到了相同的结果。

Thanks 谢谢

I don't think there is a simple Key-Value Coding method for that, but you can loop over the array and keep track of the "best" element: 我认为没有简单的键值编码方法,但是您可以遍历数组并跟踪“最佳”元素:

CLLocation *bestLocation = nil;
for (CLLocation *loc in locations) {
    if (bestLocation == nil || loc.horizontalAccuracy < bestLocation.horizontalAccuracy)
        bestLocation = loc;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM