谓词

1、谓词:

Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取。我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。

//<、>、=,<=、>=、  and、or
NSArray * array = @[@12,@34,@10,@90,@89];
NSPredicate * predicate1 = [NSPredicate predicateWithFormat:@"self >= 34 or self < 89 "];
NSArray * resultArray1 = [array filteredArrayUsingPredicate:predicate1];
//CONTAINS、BEGINSWITH、endswith : 包含谁、 以谁为前缀,以谁为后缀;
//[c]:不区分大小写 ;[d]:不区分重音符;[cd]
NSArray * array2 = [NSArray arrayWithObjects:@"erw",@"guangzhou",@"luzhou",@"郑州 00 ",@"杭州 00",@"langzhou",@"wuhan", nil];

NSPredicate * predicate2 = [NSPredicate predicateWithFormat:@"self contains[c] 'Ang'"];
NSArray * resultArray2 = [array2 filteredArrayUsingPredicate:predicate2];
    
NSPredicate * predicate3 = [NSPredicate predicateWithFormat:@"self endswith[c] 'ou'"];
NSArray * resultArray3 = [array2 filteredArrayUsingPredicate:predicate3];
//like :像
//*:0个或者多个字符;通配符
//?:1个字符;
NSArray * array2 = [NSArray arrayWithObjects:@"erw",@"guangzhou",@"luzhou",@"郑州 00 ",@"杭州 00",@"langzhou",@"wuhan", nil];
NSPredicate * predicate4 = [NSPredicate predicateWithFormat:@"self like '?州*'"];
NSArray * resultArray4= [array2 filteredArrayUsingPredicate:predicate4];
//取 交集、差集 :self in ~;not (self in ~)
NSArray * array4 = @[@34,@67,@45];
NSArray * filterArray = @[@12,@34];
NSPredicate * predicate6 = [NSPredicate predicateWithFormat:@"not (self in %@ )",filterArray];
NSArray * resultArray6 = [array4 filteredArrayUsingPredicate:predicate6];
//自定义类型
    People * p1 = [[People alloc]init];
    p1.age  =19 ;
    p1.name = @"zhangSan";
    
    People * p2 = [[People alloc]init];
    p2.age  = 29 ;
    p2.name = @"liSi";
    
    People * p3 = [[People alloc]init];
    p3.age  = 22;
    p3.name = @"zhangSanfeng";
    
    People * p4 = [[People alloc]init];
    p4.age  = 20 ;
    p4.name = @"zhang";
    
    People * p5 = [[People alloc]init];
    p5.age  = 19 ;
    p5.name = @"San";
    
    NSArray * array3 =[ NSArray arrayWithObjects:p1,p2,p3,p4,p5, nil];
    //说明过滤规则
    NSPredicate * predicate5 = [NSPredicate predicateWithFormat:@"self.name beginswith 'zhang' and self.age > 20"];
    NSArray * resultArray5 = [array3 filteredArrayUsingPredicate:predicate5];
    NSLog(@"%@",resultArray5);
//取 交集、差集 :self in ~;not (self in ~)
    NSArray * array4 = @[@34,@67,@45];
    NSArray * filterArray = @[@12,@34];
    NSPredicate * predicate6 = [NSPredicate predicateWithFormat:@"not (self in %@ )",filterArray];
    NSArray * resultArray6 = [array4 filteredArrayUsingPredicate:predicate6];
    NSLog(@"%@",resultArray6);
    
    //2、判断某一个对象是否满足条件
    NSString * str = @"eurtio";
    NSPredicate * predicate7 = [NSPredicate predicateWithFormat:@"self beginswith 'q'"];
    //判断str是否满足predicate7这个条件
    BOOL result7 =  [predicate7 evaluateWithObject:str];
    NSLog(@"%d",result7);
    
    NSNumber * num = @90 ;
    NSPredicate  * predicate8 = [NSPredicate predicateWithFormat:@"self < 80"];
    BOOL result8 = [predicate8 evaluateWithObject:num];
    NSLog(@"result8 = %d",result8);
    
    People * p = [[People alloc]init];
    p.age  =89 ;
    p.name = @"zhangSan";
    
    NSPredicate * predicate9 = [NSPredicate predicateWithFormat:@"self.age >= 80 and self.name beginswith[c] 'Zh'"];
    BOOL result9 = [predicate9 evaluateWithObject:p];
    
    NSLog(@"result9 = %d",result9);

2、正则表达式

正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式。 正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分。它可以迅速地用极简单的方式达到字符串的复杂控制。

//是否是有效的正则表达式
+ (BOOL)isValidateRegularExpression:(NSString *)strDestination byExpression:(NSString *)strExpression 
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strExpression];
    return [predicate evaluateWithObject:strDestination];
}
/验证email
+(BOOL)isValidateEmail:(NSString *)email
{
    NSString * strRegex =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,5}" ;
    NSLog(@"%@",strRegex);
    
    BOOL rt = [self isValidateRegularExpression:email byExpression:strRegex];
    return rt;
}

//验证电话号码
+(BOOL)isValidateTelNumber:(NSString *)number {
    
    NSString *strRegex = @"[0-9]{1,20}";
    
    BOOL rt = [self isValidateRegularExpression:number byExpression:strRegex];
    
    return rt;
    
}

// 正则判断手机号码地址格式
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    /**
     * 手机号码
     * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通:130,131,132,152,155,156,185,186
     * 电信:133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     * 中国移动:China Mobile
     * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     * 中国联通:China Unicom
     * 130,131,132,152,155,156,185,186
     */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     * 中国电信:China Telecom
     * 133,1349,153,180,189
     */
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     * 大陆地区固话及小灵通
     * 区号:010,020,021,022,023,024,025,027,028,029
     * 号码:七位或八位
     */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        if([regextestcm evaluateWithObject:mobileNum] == YES)
        {
            NSLog(@"China Mobile");
        }
        else if([regextestct evaluateWithObject:mobileNum] == YES)
        {
            NSLog(@"China Telecom");
        }
        else if ([regextestcu evaluateWithObject:mobileNum] == YES)
        {
            NSLog(@"China Unicom");
        }
        else
        {
            NSLog(@"Unknow");
        }
        return YES;
    }
    else
    {
        return NO;
    }
}

你可能感兴趣的:(谓词)