我有一個MKAnnotation
對象數組,稱爲arrAnnotations
。我想用與存儲在名爲「newLocation」的CLLocation
對象中的座標相同的座標挑選其中一個註釋。我想使用NSPredicate
,但它不起作用。爲什麼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:@"(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
?)。
如何使用NSPredicate
進行此項工作? 任何人都可以給我一個鏈接到一個文件,顯示Predicates如何在引擎蓋下工作? 雖然我已經閱讀並理解了蘋果的大部分Predicate Programming Guide,但我不明白他們實際上做了什麼。 正在搜索一個龐大的數組,其謂詞比循環遍歷for ... in構造更有效嗎?如果是/否,爲什麼?
你不能只將結構解釋爲'double'。你可能會用'NSLog(@「%f」,aCoordinate)得到一些東西,但這是未定義的行爲,並可能導致崩潰等。由於'NSPredicate'不能用於C-structs,因此你可以使用顯式測試除非有CoreLocation經驗的人知道更好的選擇。 – 2010-07-22 15:27:53