你好,我有一个可能很简单的问题,但我还不能解决。
- 我有一个模型类“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,但我以前从未使用过它们,而且我找不到我的问题的示例。
你们有什么提示吗?
问候 m。
最佳答案
所以每个“Location”对象都有很多“categoryIDs”?并且您想在“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]];
关于iphone - 使用 NSPredicate 匹配两个数组中的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3099730/