2010-06-23 65 views
4

您好,我有一個可能很容易的問題,但我無法處理它呢。使用NSPredicate匹配兩個數組中的ID

  • 我有一個Modelclass'Location',它保存着一個帶有Category ID的數組(12,23,56)。
  • 然後,我有一個數組與所有可用的類別ID(1,2,3,4,5,6,7,...)
  • 所有這些類別都有一個ID顯示在TableView中,並能夠選擇與否。
  • 我在MapView(註解類是位置)上顯示一個標記,它應該根據所提到的Filter TableView中的過濾器的選擇來顯示。

我需要實現「按類別過濾」功能,該功能可刪除所有標記並再次添加它們,但僅基於列表中的選擇。

所以我需要比較陣列與位置模型中的filter-id與ArrayView中的所有Filter-ID在TableView中。我用這個以下功能:

for (Location *currLocation in arrListOfLocationsNearby) { 

爲(的NSString * currKatId在currLocation.arrKatIds){

NSInteger intCurrKatId = [currKatId integerValue]; 
NSLog(@"Current KatId %@ id: %d \n", currLocation.strAdr, intCurrKatId); 

for (Filter *currFilter in [SDM getSharedDataManager].arrListOfFilterOptions) { 

if (currFilter.isSelected) { 

    NSInteger intCurrFilterId = [currFilter.strAtt_id intValue]; 
    NSLog(@"Current Filter %@ id: %d \n", currFilter.strAtt_text, intCurrFilterId); 

    if ([currKatId intValue] == [currFilter.strAtt_id intValue]) { 
    currLocation.isVisible = YES; 
    }else { 
    currLocation.isVisible = NO; 
    } 
} 
} 

}}

我知道這將是最無效的方法循環一切。我想以某種方式使用NSPredicate,但是我之前從未使用它們,而且找不到我的問題的示例。

任何提示你們?

關於m。

+0

我開始回答,感到困惑,但現在我想我再次得到它。所以基本上,你有三個數組:一個包含所有類別,一個包含用戶在tableview中選擇的類別和一個...... - 不,再次混淆。 – 2010-09-19 08:58:20

回答

0

嘗試讓每個測試的字符串表示:

NSArray *arrayOfPredicateStrings = ;// array of all tests, probably [SDM sharedDataManager].arrListOfFilterOptions 

NSPredicate *enabled = [NSPredicate predicateWithFormat:@"isSelected == true"]; 

NSArray *arrayOfEnabledPredicateStrings = 
     [arrayOfPredicateStrings filteredArrayUsingPredicate:enabled]; 

NSMutableString *formatString; 
BOOL firstTime = YES; 
for (NSString *string in arrayOfEnabledPredicateStrings) { 
    // Concatinate strings with OR inbetween 
    if (firstTime) { 
     firstTime = NO; 
     formatString = [string mutableCopy]; 
    } else { 
     [formatString appendFormat:@" OR %@", string]; 
    } 
} // formatString should look something like @"ivar1 >= 1 OR ivar2 == true OR ivar3 != \"Hello, World\"" 
NSPredicate *predicate = [NSPredicate predicateWithFormat:string]; 

NSArray *arrListOfLocationsNearby; // assume already filled 

NSArray *suitableNearbyLocations = 
     [arrListOfLocationsNearby filteredArrayUsingPrediacate:predicate]; 

如果CoreData數據存儲是您的SDM類的後面,只需創建像這樣爲獲取請求:

NSPredicate *predicate = ;// get the predicate as above 
NSManagedObjectContext *context = ;// a managed object context 
NSEntityDescription *entity = 
     [NSEntityDescription entityForName:@"entityName" inManagedObjectContext:context]; 

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
request.entity = entity; 
request.predicate = predicate; 

NSError *error; 
NSArray *results = [context executeFetchRequest:request error:&error]; 
[request release]; 

謂詞語法格式可以被發現here和使用可以發現here

6

因此,每個「位置」對象有很多「categoryIDs」?你想要顯示任何在你的「arrayOfEnabledCategoryIDs」中有一個categoryID的位置對象?

假設你有一個定位實體和類別ID的實體,而且還有它們之間的許多一對多的關係(locations <<--->> categories),你可以這樣做:

NSArray * arrayOfEnabledCategoryIDs = ...; //these are the categories to display 
NSFetchRequest * f = [[NSFetchRequest alloc] init]; 
[f setEntity:theEntityDescriptionForLocationObjects]; 
[f setPredicate:[NSPredicate predicateWithFormat:@"ANY categories.categoryID IN %@", arrayOfEnabledCategoryIDs]]; 
0

如果您熟悉的ActiveRecord來自Ruby On Rails。我一直在使用所謂的ActiveRecord +取+爲核心的一個偉大的圖書館,你可以找到更多關於它這裏http://github.com/magicalpanda/activerecord-fetching-for-core-data

NSArray *departments = [NSArray arrayWithObjects:dept1, dept2, ..., nil]; 
NSPredicate *peopleFilter = [NSPredicate predicateWithFormat:@"Department IN %@", departments]; 

NSArray *people = [Person findAllWithPredicate:peopleFilter]; 

它真的清理我的代碼。