iOS 谓词-NSPredicate

2017-01-13  本文已影响43人  草原烈鹰
谓词的各种用法:

直接上代码,看各种情况:

w01.png

1.viewController


#import "ViewController.h"
#import "Person.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSMutableArray *mutableArr = [NSMutableArray arrayWithCapacity:0];
    Person *person1 = [[Person alloc] initWithName:@"wgj001" age:10];
    Person *person2 = [[Person alloc] initWithName:@"wgj002" age:11];
    Person *person3 = [[Person alloc] initWithName:@"wgj003" age:12];

    [mutableArr addObject:person1];
    [mutableArr addObject:person2];
    [mutableArr addObject:person3];

    
//比较运算符
//    NSString *str1 = @"我们";
//    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF = %@",@"我们"];
//    if ([predicate1 evaluateWithObject:str1]) {
//        NSLog(@"testString:%@", str1);
//    }
//    
//    NSNumber *num1 = @123;
//    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF BETWEEN {123, 124}"];
//    if ([predicate2 evaluateWithObject:num1]) {
//        NSLog(@"testString:%@", num1);
//    } else {
//        NSLog(@"不符合条件");
//    }
    
//逻辑运算符 - 选出数组中特定的对象
//    NSArray *testArray = @[@1, @2, @3, @4, @5, @6];
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF > 2 && SELF < 5"];
//    NSArray *filterArray = [testArray filteredArrayUsingPredicate:predicate];
//    NSLog(@"filterArray:%@", filterArray);
    
//    NSArray *testArray = @[@"a", @"2", @"3", @"m", @"B", @"6"];
//    NSString *regular = @"^[a-z]+$";
//    NSString *regular2 = @"^[0-9]+$";
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@ || SELF MATCHES %@",regular,regular2];
//    NSArray *filterArray = [testArray filteredArrayUsingPredicate:predicate];
//    NSLog(@"filterArray:%@", filterArray);

    
//字符串比较运算符
//        NSArray *testArray = @[@"acMbi",@"itmb",@"sshh",@"ih"];
//        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH %@ || SELF ENDSWITH %@ || SELF CONTAINS %@" ,@"it",@"i",@"hh"];//开头、结尾、包含
//        NSArray *filterArray = [testArray filteredArrayUsingPredicate:predicate];
//        NSLog(@"filterArray:%@", filterArray);
    
   // MATCHES:检查某个字符串是否匹配指定的正则表达式
//    NSString *reg = @"^[a-z]+$";
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",reg];
//    if ([predicate evaluateWithObject:@"name"]) {
//        NSLog(@"wgjYES");
//    }else{
//        NSLog(@"wgjNO");
//    }
    
//    NSString *targetStr = @"(.*/page/main.do.*)";//|(.*/activity/main\\.do.*)|(.*/gift/.*\\.do.*)|(.*/alipayapi.jsp.*)|(.*/login.jsp.*)|(.*/register.jsp.*)|(.*/searh.jsp.*)|(.*/questionaire/.*\\.do.*)|(.*/eticket/.*\\.do.*)
//    
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",targetStr] ;
//    if ([predicate evaluateWithObject:@"wgj/page/main.do"]) {
//        NSLog(@"Match");
//    }else{
//        NSLog(@"NotMatch");
//    }
    
    //LIKE:检查某个字符串是否匹配指定的字符串模板;
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF LIKE '?am*'"];
//    if ([predicate evaluateWithObject:@"namewgj"]) {//"?"标示一个,“*”标示多个
//        NSLog(@"wgjYES");
//    }else{
//        NSLog(@"wgjNO");
//    }
    
//        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE '*wgj*'"];
//        if ([predicate evaluateWithObject:mutableArr[0]]) {//"?"标示一个,“*”标示多个
//            NSLog(@"wgjYES");
//        }else{
//            NSLog(@"wgjNO");
//        }
    
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE '*wgj*'"];
//    NSLog(@"%@", [mutableArr filteredArrayUsingPredicate:predicate]);


    
//集合运算符
    /*
     *  ANY,SOME:指定下列表达式中的任意元素。比如,ANY children.age < 18。
     *  ALL:指定下列表达式中的所有元素。比如,ALL children.age < 18。
     *  NONE:指定下列表达式中没有的元素。比如,NONE children.age < 18。它在逻辑上等于NOT (ANY ...)。
     *  IN:等于SQL的IN操作,左边的表达必须出现在右边指定的集合中。比如,name IN { 'Ben', 'Melissa', 'Nick' }。
     */
//    NSArray *filterArray = @[@"ab", @"abc"];
//    NSArray *array = @[@"a", @"ab", @"abc", @"abcd"];
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)", filterArray];
//    NSLog(@"%@", [array filteredArrayUsingPredicate:predicate]);
    
//    NSArray *filterArray = @[@"ab", @"abc"];
//    NSArray *array = @[@"a", @"ab", @"abc", @"abcd"];
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)", filterArray];(NOT可去掉)
//    NSLog(@"%@", [array filteredArrayUsingPredicate:predicate]);
    
//     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name in {'wgj001','zhang4'}"];
//    NSLog(@"%@", [mutableArr filteredArrayUsingPredicate:predicate]);
    
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NONE age < 10"];
//    BOOL isAll = [predicate evaluateWithObject: mutableArr];
//    NSLog(@"wgj:%ld",isAll);
    
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SOME age < 11"];
//    BOOL isAll = [predicate evaluateWithObject: mutableArr];
//    NSLog(@"wgj:%ld",isAll);
    
//对电话号码的刷选判断
//    NSString *regex = @"^[1][3-8]\\d{9}$";
//    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
//    NSLog(@"wgjwgj:%ld",[pred evaluateWithObject:@"13367876514"]);
    
    
    
    //=============语句=================
    
//    过滤对象是数组:使用- (void)filterUsingPredicate:(NSPredicate *)predicate; 针对可变数组进行过滤,过滤掉可变数组中不符合条件的。-
//    (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate; 针对不可变数组进行过滤,将符合条件的元素组成一个新数组进行返回

    
//    对单个对象进行判断过滤使用:- (BOOL)evaluateWithObject:(id)object; 向谓词对象发送该方法,参数是过滤的对象。常见于和正则表达式配合使用。
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
2. Person文件

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;

- (instancetype)initWithName:(NSString *)name age:(NSInteger)age;

@end

#import "Person.h"

@implementation Person


- (instancetype)initWithName:(NSString *)name age:(NSInteger)age
{
    self = [super init];
    if (self) {
        
        _name = name;
        _age = age;
        
    }
    return self;
    
}

@end
上一篇下一篇

猜你喜欢

热点阅读