为什么这个NSPredicate不工作? [英] Why doesn't this NSPredicate work?
问题描述
我有一个 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];
最后两次崩溃的应用程序,第三个与 如何使用 MKAnnotation协议的坐标属性是 CLLocationCoordinate2D 使用 通过快速搜索,您可以找到几个比较浮点值相等时遇到的问题的示例。您可以在此处找到一些很好的示例,这里和这里。 鉴于这一切,我相信使用代码可以解决你的问题。 I have an Array of I'm trying to use a The filteredArray always contains zero objects. I have also tried the following, which do not work either and and The last two crash the app, the third one with an How can I make this work with MKAnnotation protocol's coordinate property is a CLLocationCoordinate2D You could use 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不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! NSInvalidArgumentException
for
[NSConcreteValue compare:]
和第四个,因为 latitude
不是键值编码(我认为这是因为坐标只是一个 c-struct
而不是 NSObject
?)。 p>
NSPredicate
使这项工作?
任何人都可以给我一个链接到文档,显示Predicates如何工作?
我不明白他们实际做了什么,即使我已经阅读并理解了苹果的大多数谓词编程指南。
正在搜索一个巨大的数组,谓词比在循环中使用for ... in construct更有效率。如果是/否,为什么? 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;
}
}];
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".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];
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
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
?).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?struct
, thus it is not allowed in NSPredicate
format syntax according to the Predicate Format String SyntaxNSPredicate
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.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;
}
}];