Predicate in Obj-C

    技术2022-05-17  37

    Predicate,又一次看到了。从c++转过来的时候就开始关注它是否支持c++的STL和predicate。看到都是支持的。谁让他们都是C的超集呢。

    可以参照:

    http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html#//apple_ref/doc/uid/TP40001794

     

     

    演示下代码。

        NSMutableArray *array =         [NSMutableArray arrayWithObjects:@"Miguel", @"Ben", @"Adam", @"Melissa", nil];                 NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'a'"];         NSArray *beginWithB = [array filteredArrayUsingPredicate:bPredicate];         // beginWithB contains { @"Adam" }.                 NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'e'"];         [array filterUsingPredicate:sPredicate];            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@",                                                             [NSArray arrayWithObjects:@"Stig", @"Shaffiq", @"Chris", nil]];         BOOL result = [predicate evaluateWithObject:@"Shaffiq"];

     

     

    关于正泽表达式可以参照类

    NSRegularExpression Class Reference

    :http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html


    最新回复(0)