2014-04-08 57 views
0

我有一個數組與取自coredata的結果,它包含對象myObject與屬性myObjId快速的方式來主持陣列與不同的對象類型

,我有我的MapViewMKAnnotation,所以我有一個自定義的註釋的陣列Id

self.mapView.annotations返回對象CustomPinAnnotationmyId財產。

我想要做的是同步這兩個結果,而無需重新加載地圖上的現有引腳。如何在annotations數組中找到不在CoreData結果數組中的數據?

我寫的方法來檢查將被添加註釋權之前:

-(BOOL)isAlreadyDisplayOnMap:(NSNumber*)pinId { 
    for(CustomPinAnnotation *an in self.mapView.annotations) { 
     if(![an isKindOfClass:[MKUserLocation class]]) { 
      if([an.favourId integerValue] == [pinId integerValue]) { 
       return YES; 
      } 
     } 
    } 
    return NO; 
} 

但是,這是非常緩慢的,應該有更好的方式來比較數組。

+0

在循環是最快的循環遍歷其正向 –

+0

注意,您不必依靠地圖視圖的'annotations'陣列上知道註釋在地圖上。您可以隨時在地圖上保留annotations的_own_「list」,但支持通過pinId支持_immediate_查詢的結構(就像NSDictionary的關鍵字爲pinId一樣)。 – Anna

+0

完全不相關,但在現有的代碼中,我會建議更改'if'條件來檢查對象**是**您正在尋找的類('CustomPinAnnotation')而不是檢查它是否不是* *'MKUserLocation'。通過這種方式,您可以更加確信自定義屬性訪問不會失敗,並且可能會導致可能不止兩種類的類型。 – Anna

回答

1

您可能需要使用塊嘗試:

 __block BOOL alreadyDisplayed; 
    [self.mapView.annotations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
     CustomPinAnnotation *an = (CustomPinAnnotation *)obj; 
      if(![an isKindOfClass:[MKUserLocation class]]) { 
      if([an.favourId integerValue] == [pinId integerValue]) { 
       alreadyDisplayed = YES; 
       *stop = YES; 
      } 
      } 
    }]; 
相關問題