ios 谓词

(1)比较运算符>,<,==,>=,<=,!=可用于数值及字符串例:@"number > 100"

(2)范围运算符:IN、BETWEEN例:@"number BETWEEN {1,5}" @"address IN {'shanghai','beijing'}"

(3)字符串本身:SELF 例:@“SELF == ‘APPLE’"

(4)字符串相关:BEGINSWITH、ENDSWITH、CONTAINS例:@"name CONTAIN[cd] 'ang'" //包含某个字符串

@"name BEGINSWITH[c] 'sh'" //以某个字符串开头 @"name ENDSWITH[d] 'ang'"

//以某个字符串结束注:[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。

(5)通配符:LIKE例:@"name LIKE[cd] '*er*'" //*代表通配符,Like也接受[cd]. @"name LIKE[cd] '???er*'"

(6)正则表达式:MATCHES例:NSString *regex = @"^A.+e$"; //以A开头,e结尾 @"name MATCHES %@",regex"

- (void)testPredicate {
    NSArray *arr = @[@3,@99,@6,@0];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self > 4"];
    // 注意这个语法里面字符串要用单引号
    
    NSArray *result = [arr filteredArrayUsingPredicate:predicate];
    // 打印输出结果 99, 6
    NSLog(@"%@",result);
    
    NSArray *arr1 = @[@"ss",@"dddd",@"apple"];
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"self == 'apple'"];
    // 注意这个语法里面字符串要用单引号
    
    NSArray *result1 = [arr1 filteredArrayUsingPredicate:predicate1];
    // 打印输出结果:apple
    NSLog(@"%@",result1);
}

- (void)testPreditadeForCD {
//    TFCityAddressModel *info = [[TFCityAddressModel alloc]init];
//    info.cityName = @"上海";
//    info.cityPinYin = @"上海市";
//    info.cityPinYinHead = @"上海市";
//
//    TFCityAddressModel *info1 = [[TFCityAddressModel alloc]init];
//    info1.cityName = @"杭州市";
//    info1.cityPinYin = @"杭州";
//    info1.cityPinYinHead = @"杭州";
//
//    TFCityAddressModel *info2 = [[TFCityAddressModel alloc]init];
//    info2.cityName = @"重庆";
//    info2.cityPinYin = @"重庆";
//    info2.cityPinYinHead = @"重庆";
    
    NSArray *_cityAddressArray = @[@"杭州市", @"重庆", @"上海市"];
    NSString *searchBarText = @"上海";
     NSString *searchBarText1 = @"杭州";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(self CONTAINS[cd] %@) OR (self CONTAINS[cd] %@) OR (self CONTAINS[cd] %@)", searchBarText, searchBarText, searchBarText1];
    NSArray *array = [_cityAddressArray filteredArrayUsingPredicate:predicate];

    NSMutableArray *_searchList = [[NSMutableArray alloc]init];
    if (array.count > 0) {
        for (NSString *cityText in array) {
            [_searchList addObject: cityText];
        }
    }
    //打印输出结果: 杭州市, 上海市
    NSLog(@"%@",_searchList);
}
相关文章
相关标签/搜索