filter筛选数据

前段时间的开发有个业务场景是:后台一次返回全量的题目数据(每道题目都有对应的questionID),然后在某个场景下后台返回若干题目的questionID,需要前台自己去缓存好的全量题目数据中筛选查找。不多说,先贴出代码:

/**
 题目model
 */
@interface QuestionModel : NSObject
@property (nonatomic, copy) NSString *questionId;/**< 题目ID */
@property (nonatomic, copy) NSString *content;/**< 题目内容 */
@end

@implementation QuestionModel
- (NSString *)description {
    return [NSString stringWithFormat:@"questionId = %@ content = %@",_questionId ,_content];
}
@end

#import "ViewController.h"
#import "QuestionModel.h"
@interface ViewController ()
@property (nonatomic, copy) NSMutableArray *questionArr;/**< 题目数据 */
@property (nonatomic, copy) NSArray *selectedQuestionIDArr;/**< 待选出的题目的IDArr */

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (!_questionArr) {
        _questionArr = @[].mutableCopy;
    }
    if (!_selectedQuestionIDArr) {
        _selectedQuestionIDArr = @[@"001",@"007",@"006",@"111"];
    }
    NSArray *questionArr = @[@{@"questionId":@"001",
                               @"content":@"下列加粗字的读音完全正确的一组是"},
                             @{@"questionId":@"002",
                               @"content":@"下列句子中加粗的成语使用正确的一项是"},
                             @{@"questionId":@"003",
                               @"content":@"下列选项中,加粗词语使用得不恰当的是"},
                             @{@"questionId":@"004",
                               @"content":@"加粗字的注音,完全正确的一项是"},
                             @{@"questionId":@"005",
                               @"content":@"下列词语中,加下划线字的读音不同的一项是(  )"},
                             @{@"questionId":@"006",
                               @"content":@"以下划线的关于“春”字的成语使用不正确的一项是"},
                             @{@"questionId":@"007",
                               @"content":@"选择加下划线的字的字义。近种篱边菊,秋天未著花。"},
                             @{@"questionId":@"008",
                               @"content":@"下面选项中说法正确的一项是"},
                             @{@"questionId":@"009",
                               @"content":@"下面加点字的读音完全相同的一项是"},
                             @{@"questionId":@"010",
                               @"content":@"加点字的读音全部正确的一项是"}];
    for (NSDictionary *dic in questionArr) {
        QuestionModel *model = [[QuestionModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.questionArr addObject:model];
    }
}

/**
 自己单层循环遍历匹配出题目
 特点:遍历次数最多n次,匹配出的题目顺序是以题库顺序为准(打印题目顺序为1、6、7)
 */
- (NSArray *)normalMatchQuestion {
    __block NSMutableArray *selectedQuestionMulArr = @[].mutableCopy;
    typeof(self) __weak weakSelf = self;
    [self.questionArr enumerateObjectsUsingBlock:^(QuestionModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([weakSelf.selectedQuestionIDArr containsObject:obj.questionId]) {
            [selectedQuestionMulArr addObject:obj];
            if (selectedQuestionMulArr.count == weakSelf.selectedQuestionIDArr.count) {
                *stop = YES;
            }
        }
    }];
    NSLog(@"normalMatchQuestion selectedQuestionMulArr == %@",selectedQuestionMulArr);
    return [NSArray arrayWithArray:selectedQuestionMulArr];
}

/**
 自己双层嵌套循环遍历匹配出题目
 特点:遍历次数最多m*n次,匹配出的题目顺序是以选题顺序为准(打印题目顺序为1、7、6)
 */
- (NSArray *)normalDoubleTraversalMatchQuestion {
    __block NSMutableArray *selectedQuestionMulArr = @[].mutableCopy;
    typeof(self) __weak weakSelf = self;
    [self.selectedQuestionIDArr enumerateObjectsUsingBlock:^(NSString * _Nonnull questionID, NSUInteger questionIDIdx, BOOL * _Nonnull questionIDStop) {
        [weakSelf.questionArr enumerateObjectsUsingBlock:^(QuestionModel * _Nonnull questionModel, NSUInteger questionModelIdx, BOOL * _Nonnull questionModelStop) {
            if ([questionModel.questionId isEqualToString:questionID]) {
                [selectedQuestionMulArr addObject:questionModel];
                if (questionIDIdx == weakSelf.selectedQuestionIDArr.count - 1) {
                    *questionModelStop = YES;
                }
            }
        }];
    }];
    NSLog(@"normalDoubleTraversalMatchQuestion selectedQuestionMulArr == %@",selectedQuestionMulArr);
    return [NSArray arrayWithArray:selectedQuestionMulArr];
}

/**
 自己利用字典特性两次单层循环遍历匹配出题目
 特点:遍历次数最多为2n次,匹配出的题目顺序是以选题顺序为准(打印题目顺序为1、7、6)
 */
- (NSArray *)twiceTraversalmatchQuestion {
    __block NSMutableDictionary *questionDataDic = @{}.mutableCopy;
    for (QuestionModel *questionModel in self.questionArr) {
        [questionDataDic setValue:questionModel forKey:questionModel.questionId];
    }
    
    __block NSMutableArray *selectedQuestionMulArr = @[].mutableCopy;
    [self.selectedQuestionIDArr enumerateObjectsUsingBlock:^(NSString * _Nonnull questionID, NSUInteger questionIDIdx, BOOL * _Nonnull questionIDStop) {
        if ([questionDataDic objectForKey:questionID]) {
            QuestionModel *questionModel = [questionDataDic objectForKey:questionID];
            [selectedQuestionMulArr addObject:questionModel];
        }
    }];
    NSLog(@"twiceTraversalmatchQuestion selectedQuestionMulArr == %@",selectedQuestionMulArr);
    return [NSArray arrayWithArray:selectedQuestionMulArr];
}

/**
 自己利用NSPredicate匹配出题目
 特点:匹配出的题目顺序是以题库顺序为准(打印题目顺序为1、7、6)
 */
- (NSArray *)matchQuestion{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"questionId in %@", self.selectedQuestionIDArr];
    NSArray *list = [self.questionArr filteredArrayUsingPredicate:predicate];
    NSLog(@"twiceTraversalmatchQuestion selectedQuestionMulArr == %@",list);
    return list;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//    [self normalMatchQuestion];
//    [self normalDoubleTraversalMatchQuestion];
//    [self twiceTraversalmatchQuestion];
//    [self matchQuestion];
}

@end

比较容易看出,有两种筛选结果分别是:
以题库题目顺序(打印题目顺序为1、6、7);
以选题题目顺序(打印题目顺序为1、7、6)。
其中使用谓词(谓词NSPredicate用法)筛选的方法最为简便,不过本人鄙陋,不知道用NSPredicate怎么写可以按照选题题目顺序返回数据,如果哪位朋友知道,望告知,不胜感激。

你可能感兴趣的:(filter筛选数据)