macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

NSComparisonPredicateクラス

INDEX>Foundation>NSComparisonPredicate

apple(mac)
apple(iOS)

解説

継承 NSPredicate : NSObject
準拠 NSCoding (NSPredicate)
NSCopying (NSPredicate)
NSObject (NSObject)
フレームワーク /System/Library/Frameworks/Foundation.framework
使用可能 Mac OS X v10.4以降,iOS 3.0以降
定義 NSComparisonPredicate.h

概要

比較する式を表現するためのクラスです。NSPredicateで使用する age >=30 などの表現を作成することができます。

作成

比較記述式の作成

検索キーと式の左項、右項、演算子NSPredicateオブジェクトを作成するには(+ predicateWithLeftExpression:rightExpression:modifier:type:options)メソッドを使用します。比較演算をセレクタで指定してNSPredicateオブジェクトを作成するには(+ predicateWithLeftExpression:rightExpression:customSelector)メソッドを使用します。
allocで作成してから検索キーと式の左項、右項、演算子でNSPredicateオブジェクトを初期化するには(– initWithLeftExpression:rightExpression:modifier:type:options)メソッドを使用します。比較演算をセレクタで指定してNSPredicateオブジェクトを初期化するには(– initWithLeftExpression:rightExpression:customSelector)メソッドを使用します。

ダミーのリストの中からageが20以上40未満の人物を抽出するサンプル

#pragma mark NSPredicate:predicateWithFormat:
-(void)method001
{
    //age
    NSExpression *lhs = [NSExpression expressionForKeyPath:@"age"];
    
    //20
    NSExpression *greaterThanRhs = [NSExpression expressionForConstantValue:[NSNumber numberWithInt:20]];
    
    //age >= 20
    NSPredicate *greaterThanPredicate = [NSComparisonPredicate
 predicateWithLeftExpression:lhs
 rightExpression:greaterThanRhs
 modifier:NSDirectPredicateModifier
 type:NSGreaterThanOrEqualToPredicateOperatorType //>=
 options:0];
    
    //40
    NSExpression *lessThanRhs = [NSExpression expressionForConstantValue:[NSNumber numberWithInt:40]];
    //age < 40
    NSPredicate *lessThanPredicate = [NSComparisonPredicate
      predicateWithLeftExpression:lhs
      rightExpression:lessThanRhs
      modifier:NSDirectPredicateModifier
      type:NSLessThanPredicateOperatorType // <
      options:0];
    //age >= 20 AND age < 40
    NSPredicate *comPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:
      [NSArray arrayWithObjects:greaterThanPredicate, lessThanPredicate, nil]];

    NSDictionary *tanaka = [[NSDictionary alloc] initWithObjectsAndKeys:
    [NSNumber numberWithInt:30],@"age" ,
    @"Tanaka",@"lastName",@"Taro",@"firstName",nil];
    NSDictionary *sato = [[NSDictionary alloc] initWithObjectsAndKeys:
  [NSNumber numberWithInt:43],@"age" ,
  @"Sato",@"lastName",@"Satoko",@"firstName",nil];
    NSDictionary *suzuki = [[NSDictionary alloc] initWithObjectsAndKeys:
    [NSNumber numberWithInt:39],@"age" ,
    @"Suzuki",@"lastName",@"Ichiro",@"firstName",nil];
    NSDictionary *yamada = [[NSDictionary alloc] initWithObjectsAndKeys:
    [NSNumber numberWithInt:12],@"age" ,
    @"Yamada",@"lastName",@"Jiro",@"firstName",nil];
    
    NSArray *anArray =
    [[NSArray alloc] initWithObjects:tanaka,sato,suzuki,yamada,nil];
    
    NSArray *aResult = [anArray filteredArrayUsingPredicate:comPredicate];
    
    NSLog(@"%s %@",__FUNCTION__,[aResult description]);
    NSLog(@"%s %@", __FUNCTION__,[comPredicate predicateFormat]);
}

オブジェクトの情報を取得する

レシーバの比較記述式装飾子(日本語にすると何のことかわかりませんね、comparison predicate modifier)を返すには(– comparisonPredicateModifier)メソッドを使います。

NSDirectPredicateModifier
直接左辺と右辺を比較する際に使用します。
NSAllPredicateModifier
対多のリレーションシップのすべての要素を比較する際に使用します。
NSAnyPredicateModifier
対多のリレーションシップの任意の要素を比較する際に使用します。

レシーバのカスタムセレクタを取得するには(– customSelector)メソッドを使います。SEL型で返ってきますので文字列にするにはNSStringFromSelector()を使用します。
レシーバの左辺を取得するには(– leftExpression)メソッドを、右辺を取得するには(– rightExpression)メソッドを使用します。レシーバの演算子タイプを取得するには(– predicateOperatorType)メソッドを使用します。

演算子タイプ
NSLessThanPredicateOperatorType
<

NSLessThanOrEqualToPredicateOperatorType
<=

NSGreaterThanPredicateOperatorType
>

NSGreaterThanOrEqualToPredicateOperatorType
>=

NSEqualToPredicateOperatorType
==

NSNotEqualToPredicateOperatorType
!=

NSMatchesPredicateOperatorType
MATCHES

NSLikePredicateOperatorType
LIKE

NSBeginsWithPredicateOperatorType
BEGINSWITH

NSEndsWithPredicateOperatorType
ENDSWITH

NSInPredicateOperatorType
IN

NSCustomSelectorPredicateOperatorType
カスタムセレクタを使用

オプションを取得するには(– options)メソッドを使用します。

●オプション
NSCaseInsensitivePredicateOption
大文字小文字を区別しない([c]オプション)
NSDiacriticInsensitivePredicateOption
発音記号を区別しない([d]オプション)
NSNormalizedPredicateOption
上記2つに代わって前処理される([n]オプション)

適合するプロトコル

サブクラス化の注意

参照