4

您好,我有一个可能很简单的问题,但我还不能处理。

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

我需要实现一个“按类别过滤”功能,该功能删除所有标记并再次添加它们,但仅基于列表中的选择。

所以我需要将数组与位置模型中的过滤器 ID 与数组与 TableView 中的所有过滤器 ID 进行比较。我为此使用了以下功能:

for (Location *currLocation in arrListOfLocationsNearby) {

for (NSString *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;
  }
 }
}

} }

我知道这将是循环遍历所有内容的最无效方法。我想以某种方式使用 NSPredicate ,但我以前从未使用过它们,而且我找不到我的问题的例子。

你们有什么提示吗?

问候米。

4

3 回答 3

6

那么每个“Location”对象都有很多“categoryID”吗?并且您想在您的“”中显示任何具有 categoryID 的 Location 对象arrayOfEnabledCategoryIDs

假设您有一个 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]];
于 2010-06-26T03:41:57.973 回答
0

如果您熟悉 Ruby On Rails 中的 ActiveRecord。我一直在使用一个很棒的库,叫做 activerecord+fetching+for+core,你可以在这里找到更多关于它的信息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];

它确实清理了我的代码。

于 2010-09-15T08:18:22.063 回答
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];

谓词格式语法可以在这里找到,用法可以在这里找到

于 2010-06-23T08:49:07.130 回答