使用NSPredicate在两个数组中匹配id

gro*_*ald 4 iphone objective-c nspredicate

你好,我有一个简单的问题但我无法处理它.

  • 我有一个Modelclass'Location',它包含一个带有Category ID(12,23,56)的数组.
  • 然后我有一个包含所有可用类别ID的数组(1,2,3,4,5,6,7,...)
  • 所有这些类别的ID都显示在TableView中,并且能够选择与否.
  • 我在MapView(注释类是位置)上显示了一个标记,它应该根据所提到的Filter TableView中过滤器的选择来显示.

我需要实现一个"按类别筛选"功能,该功能会删除所有标记并再次添加它们,但这只是基于列表中的选择.

所以我需要将数组与位置模型中的filter-id进行比较,并将数组与TableView中的所有Filter-ID进行比较.我使用了以下功能:

for (Location *currLocation in arrListOfLocationsNearby) {
Run Code Online (Sandbox Code Playgroud)

for(NSrtring*currKatId in 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;
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

}}

我知道这将是循环通过一切的最无效的方式.我想以某种方式使用NSPredicate,但我以前从未使用它们,我找不到我的问题的例子.

你们有什么提示吗?

关于m.

Dav*_*ong 6

那么每个"Location"对象都有很多"categoryID"?并且您想要显示任何在" arrayOfEnabledCategoryIDs"中包含categoryID的Location对象?

假设您有一个Location实体和一个CategoryID实体,并且它们之间存在多对多关系(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]];
Run Code Online (Sandbox Code Playgroud)