为什么这个NSPredicate不起作用?

时间:2010-07-22 14:59:07

标签: cocoa performance comparison predicate nspredicate

我有一个名为MKAnnotation的{​​{1}}个数组。我想挑选一个注释,其坐标与存储在名为“newLocation”的arrAnnotations对象中的坐标相同。

我正在尝试使用CLLocation,但它不起作用。

NSPredicate

filteredArray始终包含零个对象。

我也尝试了以下哪些无法正常工作

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

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

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

最后两个崩溃应用程序,第三个崩溃NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(coordinate.latitude == %f)", newLocation.coordinate.latitude]; NSInvalidArgumentException和第四个,因为[NSConcreteValue compare:]不符合键值编码(我假设这是因为坐标只是latitude而不是c-struct?)。

如何使用NSObject进行此操作? 任何人都可以给我一个链接到一个文件,显示Predicates如何在幕后工作? 我不明白他们实际上做了什么,尽管我已经阅读并理解了Apple的大部分Predicate Programming Guide。 正在使用for ... in构造中搜索带有谓词的大型数组比使用谓词循环更有效吗?如果是/否,为什么?

1 个答案:

答案 0 :(得分:6)

MKAnnotation协议的坐标属性为CLLocationCoordinate2D struct,因此根据Predicate Format String Syntax

不允许采用NSPredicate格式语法

您可以使用NSPredicate predicateWithBlock:来完成您要执行的操作,但是您要小心CLLocationCoordinate2D以及如何比较它是否相等。

CLLocationCoordinate2D的{​​{1}}和latitude属性为CLLocationDegrees数据类型,根据定义为longitude

通过快速搜索,您可以找到在比较浮点值是否相等时将遇到的几个问题示例。可以找到一些好的示例hereherehere

鉴于这一切,我相信对你的谓词使用代码可能会解决你的问题。

double