LoginSignup
8
3

More than 5 years have passed since last update.

NSPredicateで絞り込みやindexを取得するで🤜🤛

Last updated at Posted at 2017-12-19

NSPredicateで絞り込みやindexを取得するで🤜🤛

Objective-C にはNSPredicateという配列の絞り込みに便利なクラスがあります。
普通の使い方は検索したら結構載っているので
今回は普通の使い方+あまり他で載っていない使い方をご紹介します。
気まぐれでたまに他のパターンも増やすかも。

絞り込み元になるDictionaryの配列

.m
- (NSArray *)array
{
    return @[@{@"fruit":@"りんご"},
             @{@"fruit":@"パイナップル"},
             @{@"fruit":@"いちご"},
             @{@"fruit":@"りんご"},
             @{@"fruit":@"ぶとう"},
             @{@"fruit":@"りんご"},
             @{@"fruit":@"オレンジ"},
             @{@"fruit":@"さくらんぼ"},
             @{@"fruit":@""},
             @{@"fruit":[NSNull null]}];
}

普通に絞り込む

.m
- (void)hoge
{
    NSArray *fruitArray = [self array];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K= %@",@"fruit",@"りんご"];
    NSArray *filterArray = [fruitArray filteredArrayUsingPredicate: predicate];
    NSLog(@"%@",filterArray);
    /* logの出力結果
     (
     {fruit = "りんご";},
     {fruit = "りんご";},
     {fruit = "りんご";}
     )
     */

     predicate = [NSPredicate predicateWithFormat:@"%K = %@ OR %K = %@",@"fruit",@"りんご",@"fruit",@"パイナップル"];
     filterArray = [fruitArray filteredArrayUsingPredicate: predicate];
     NSLog(@"%@",filterArray);
     /* logの出力結果
      (
      {fruit = "りんご";},
      {fruit = "パイナップル";},
      {fruit = "りんご";},
      {fruit = "りんご";}
      )
      */

}

Indexを取得する

.m
- (void)hoge
{
    NSArray *fruitArray = [self array];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@",@"fruit",@"オレンジ"];
    NSUInteger index = [fruitArray indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
        return [predicate evaluateWithObject:obj];
    }];
    if (index != NSNotFound) {
        NSLog(@"該当するIndex:%ld",(long)index);
    }
    /* logの出力結果
     該当するIndex:6
     */
}

IndexSet(indexのリスト)を取得する

.m
- (void)hoge
{
    NSArray *fruitArray = [self array];
    NSIndexSet *indexSet = [fruitArray indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
        return [predicate evaluateWithObject:obj];
    }];

    NSLog(@"%@",indexSet);
    /* logの出力結果
     [number of indexes: 3 (in 3 ranges), indexes: (0 3 5)]
     */
}

文字数で絞り込む

.m
- (void)hoge
{
    // 3文字で絞る
    NSArray *fruitArray = [self array];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K.length = 3",@"fruit"];
    NSArray *filterArray = [fruitArray filteredArrayUsingPredicate: predicate];
    NSLog(@"%@",filterArray);
    /* logの出力結果
     (
     {fruit = "りんご";},
     {fruit = "いちご";},
     {fruit = "りんご";},
     {fruit = "ぶどう";},
     {fruit = "りんご";}
     )
     */

    // 空文字以外(NULLは含まれる)
    predicate = [NSPredicate predicateWithFormat:@"%K.length != 0",@"fruit"];
    filterArray = [fruitArray filteredArrayUsingPredicate: predicate];
    NSLog(@"%@",filterArray);
    /* logの出力結果
     (
     {fruit = "りんご";},
     {fruit = "パイナップル";},
     {fruit = "いちご";},
     {fruit = "りんご";},
     {fruit = "ぶどう";},
     {fruit = "りんご";},
     {fruit = "オレンジ";},
     {fruit = "さくらんぼ";},
     {fruit = "<null>";}
     )
     */

    // 空文字とNULL以外
    predicate = [NSPredicate predicateWithFormat:@"%K.length != 0 AND %K != NULL",@"fruit",@"fruit"];
    filterArray = [fruitArray filteredArrayUsingPredicate: predicate];
    NSLog(@"%@",filterArray);
    /* logの出力結果
     (
     {fruit = "りんご";},
     {fruit = "パイナップル";},
     {fruit = "いちご";},
     {fruit = "りんご";},
     {fruit = "ぶどう";},
     {fruit = "りんご";},
     {fruit = "オレンジ";},
     {fruit = "さくらんぼ";}
     )
     */

}

あいまい検索で絞り込み

.m
- (void)hoge
{
    // *レン* であいまい検索
    NSArray *fruitArray = [self array];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K LIKE %@",@"fruit",[NSString stringWithFormat:@"*%@*",@"レン"]];
    NSArray *filterArray = [fruitArray filteredArrayUsingPredicate: predicate];
    NSLog(@"%@",filterArray);
    /* logの出力結果
     (
     {fruit = "オレンジ";}
     )
     */
}

8
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3