Objective-C述語

4595 ワード

まず、本文は筆者のために「Objective-C基礎教程」のノートを学び、筆者自身の理解とまとめを加えた.NSPredicateクラスは、フィルタの条件を指定し、各オブジェクトをフィルタし、条件と一致するかどうかを判断します.

1.述語の作成

predicateWithFormat述語を作成します.
+ (id) predicateWithFormat: (NSString*) format...;
evaluateWithObjectオブジェクトを受け入れ、指定されたオブジェクトに基づいて自身の値を計算します.
- (BOOL) evaluateWithObject: (id) value ;
filteredArrayUsingPredicateは、NSArray配列のカテゴリメソッドであり、配列の内容をフィルタします.
@interface ShapeBounds : NSObject {
    int width;
    int height;
}

@property int width;
@property int height;

@end

@implementation ShapeBounds

@synthesize width;
@synthesize height;

- (NSString*) description {
    return [NSString stringWithFormat: @"(%d, %d)", width, height];
}

@end

@interface Shape : NSObject {
    ShapeBounds* bounds;
}

@property (readwrite, retain) ShapeBounds* bounds;

@end

@implementation Shape

@synthesize bounds;

- (id) init {
    if (self = [super init]) {
        bounds = [[ShapeBounds alloc] init];
    }
    return self;
}

- (NSString*) description {
    return [NSString stringWithFormat: @"Shape bounds = %@", bounds];
}	
@end

Shape* createShape(int width, int height) {
    Shape* shape = [[Shape alloc] init];

    shape.bounds.width = width;
    shape.bounds.height = height;

    return shape;
}

int main(int argc, const char* argv[]) {
    @autoreleasepool {
        ShapeBounds* bounds = [[ShapeBounds alloc] init];
        bounds.width = 20;
        NSPredicate* predicate = [NSPredicate predicateWithFormat: @"width == 20"];
        if ([predicate evaluateWithObject: bounds]) {
            NSLog(@"evaluateWithObject true");
        }

        predicate = [NSPredicate predicateWithFormat: @"bounds.width == 20"];
        NSMutableArray* array = [NSMutableArray arrayWithCapacity: 4];
        [array addObject: createShape(20, 30)];
        [array addObject: createShape(40, 50)];
        [array addObject: createShape(20, 45)];

        NSArray* result = [array filteredArrayUsingPredicate: predicate];
        NSLog(@"%@", result);
    }
    return 0;
}

2.フォーマット説明子


述語文字列にフォーマット説明子を使用できます.
predicate = [NSPredicate predicateWithFormat: @"width == %d", 20];
%Kキーパスの指定
predicate = [NSPredicate predicateWithFormat: @"%K == %d", @"width", 20];
$シンボルは変数を指定し、NSDictionaryにデータを追加し、predicateWithSubstitutionVariablesを使用して新しい述語を構築します.
predicateTemplate = [NSPredicate predicateWithFormat: @"width == $WIDTH"];
predicate = [predicateTemplate predicateWithSubstitutionVariables: dict];

int main(int argc, const char* argv[]) {
    @autoreleasepool {
        ShapeBounds* bounds = [[ShapeBounds alloc] init];
        bounds.width = 20;
        NSPredicate* predicate = [NSPredicate predicateWithFormat: @"width == %d", 20];
        if ([predicate evaluateWithObject: bounds]) {
            NSLog(@"evaluateWithObject true");
        }

        NSPredicate* predicate1 = [NSPredicate predicateWithFormat:
                @"%K == %d", @"width", 20];
        if ([predicate1 evaluateWithObject: bounds]) {
            NSLog(@"evaluateWithObject true");
        }

        NSPredicate* predicateTemplate = [NSPredicate predicateWithFormat:
                @"width == $WIDTH"];
        NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys: 
                [NSNumber numberWithInt: 20], @"WIDTH", nil];
        NSPredicate* predicate2 = [predicateTemplate 
                predicateWithSubstitutionVariables: dict];
        if ([predicate2 evaluateWithObject: bounds]) {
            NSLog(@"evaluateWithObject true");
        }

    }
    return 0;
}

3.演算子


述部文字列は比較演算子(>,>=,
int main(int argc, const char* argv[]) {
    @autoreleasepool {
        ShapeBounds* bounds = [[ShapeBounds alloc] init];
        bounds.width = 20;
        bounds.height = 30;
        NSPredicate* predicate = [NSPredicate predicateWithFormat: @"width > 10"];
        if ([predicate evaluateWithObject: bounds]) {
            NSLog(@"evaluateWithObject >");
        }

        predicate = [NSPredicate predicateWithFormat: @"width < 30"];
        if ([predicate evaluateWithObject: bounds]) {
            NSLog(@"evaluateWithObject