为什么这个NSPredicate不工作? [英] Why doesn't this NSPredicate work?

查看:139
本文介绍了为什么这个NSPredicate不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MKAnnotation 数组 arrAnnotations 。我想选择与存储在名为newLocation的 CLLocation 对象中相同坐标的注释之一。



我尝试使用 NSPredicate ,但它不起作用。

  NSPredicate * predicate = [NSPredicate predicateWithFormat:@(SELF.coordinate ==%f),newLocation.coordinate]; 
NSArray * filteredArray = [arrAnnotations filteredArrayUsingPredicate:predicate];
[self.mapView selectAnnotation:[filteredArray objectAtIndex:0] animated:YES];

filteredArray总是包含零个对象。



我也尝试了下面的,不工作

  NSPredicate * predicate = [NSPredicate predicateWithFormat:@ =%f),newLocation.coordinate]; 

  NSPredicate * predicate = [NSPredicate predicateWithFormat:@(coordinate> 0)]; 

  NSPredicate * predicate = [NSPredicate predicateWithFormat:@(coordinate.latitude ==%f),newLocation.coordinate.latitude]; 

最后两次崩溃的应用程序,第三个与 NSInvalidArgumentException for [NSConcreteValue compare:] 和第四个,因为 latitude 不是键值编码(我认为这是因为坐标只是一个 c-struct 而不是 NSObject ?)。 p>

如何使用 NSPredicate 使这项工作?
任何人都可以给我一个链接到文档,显示Predicates如何工作?
我不明白他们实际做了什么,即使我已经阅读并理解了苹果的大多数谓词编程指南
正在搜索一个巨大的数组,谓词比在循环中使用for ... in construct更有效率。如果是/否,为什么?

解决方案

MKAnnotation协议的坐标属性是 CLLocationCoordinate2D struct ,因此不允许在 NSPredicate 格式语法中根据谓词格式字符串语法



使用 NSPredicate predicateWithBlock:而不是完成你想做的,但是你小心 CLLocationCoordinate2D 以及如何比较它的相等性。



CLLocationCoordinate2D 纬度 longitude 属性为 CLLocationDegrees 数据类型,根据定义它是 double



通过快速搜索,您可以找到几个比较浮点值相等时遇到的问题的示例。您可以在此处找到一些很好的示例,这里这里



鉴于这一切,我相信使用代码可以解决你的问题。

  NSPredicate * predicate = [NSPredicate predicateWithBlock:^ BOOL(id calculatedObject,NSDictionary * bindings){

id< MKAnnotation> annotation = evaluatedObject;

if(fabsf([annotation coordinate] .latitude - [newLocation coordinate] .latitude)<0.000001
&& fabsf([annotation coordinate] .longitude - [newLocation coordinate] .longitude)< 0.000001){
return YES;
} else {
return NO;
}

}];


I have an Array of MKAnnotation objects called arrAnnotations. I want to pick out one of the annotations with the same coordinate as the one stored in a CLLocation object called "newLocation".

I'm trying to use a NSPredicate, but it doesn't work.

NSPredicate *predicate = [NSPredicate  predicateWithFormat:@"(SELF.coordinate == %f)", newLocation.coordinate];
NSArray* filteredArray = [arrAnnotations filteredArrayUsingPredicate:predicate];
[self.mapView selectAnnotation:[filteredArray objectAtIndex:0] animated:YES];

The filteredArray always contains zero objects.

I have also tried the following, which do not work either

NSPredicate *predicate = [NSPredicate  predicateWithFormat:@"(coordinate == %f)", newLocation.coordinate];

and

NSPredicate *predicate = [NSPredicate  predicateWithFormat:@"(coordinate > 0)"];

and

NSPredicate *predicate = [NSPredicate  predicateWithFormat:@"(coordinate.latitude == %f)", newLocation.coordinate.latitude];

The last two crash the app, the third one with an NSInvalidArgumentException for [NSConcreteValue compare:] and the fourth, because latitude is not key-value-coding-compliant (I assume this is because coordinate is just a c-struct and not an NSObject?).

How can I make this work with NSPredicate? Can anyone give me a link to a document that shows how Predicates work under the hood? I don't understand what they actually do, even though I have read and understood most of Apple's Predicate Programming Guide. Is searching a huge array with predicates more efficient than just looping through it with a for...in construct? If yes/no, why?

解决方案

MKAnnotation protocol's coordinate property is a CLLocationCoordinate2D struct, thus it is not allowed in NSPredicate format syntax according to the Predicate Format String Syntax

You could use NSPredicate predicateWithBlock: instead to accomplish what you are trying to do, but you have be careful with CLLocationCoordinate2D and how to compare it for equality.

CLLocationCoordinate2D's latitude and longitude property are CLLocationDegrees data type, which is a double by definition.

With a quick search you can find several examples of problems you would face when comparing floating point values for equality. Some good examples can be found here, here and here.

Given all that, I believe that using code bellow for your predicate might solve your problem.

NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {

    id<MKAnnotation> annotation = evaluatedObject;

    if (fabsf([annotation coordinate].latitude - [newLocation coordinate].latitude) < 0.000001 
            && fabsf([annotation coordinate].longitude - [newLocation coordinate].longitude) < 0.000001) {
        return YES;
    } else {
        return NO;
    }

}];

这篇关于为什么这个NSPredicate不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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