相关文章推荐
行走的苹果  ·  column_constraint ...·  4 月前    · 
低调的沙发  ·  Inno ...·  1 年前    · 
斯文的豆浆  ·  把 ChatGPT 接入到 Siri ...·  1 年前    · 

Predicates wrap some combination of expressions and operators and when evaluated return a BOOL.
The NSPredicate class is used to define logical conditions used to constrain a search either for a fetch or for in-memory filtering.
// 大概是说: Predicate根据组合表达式的计算结果返回一个BOOL值. 可用于约束搜索或内存过滤.

Creating a Predicate:

+ (NSPredicate *)predicateWithFormat:(NSString *)format, ...

format:

Predicate Format String Syntax:

PS: apple 称这个 format 为文本解释器(predicate string parser), 和正则表达式完全不是一个东西.

空格和关键字大小写不敏感, 支持括号嵌套的表达式, 不做类型检查;
$variable 表示变量, ?是非法关键字;
支持 printf 网络的格式说明符, 其中%k和%@很重要.

%@ 对象值占位符, 一般是一个字符串, 数字, 日期等.
%K key path占位符,(key path 是什么? 看看KVC和KVO, 访问对象属性的技术.)

用%@指定字符串变量时, 它会被解释为带双引号的字符串, 用%K指定一个可变的属性

NSString *attributeName  = @"firstName";
NSString *attributeValue = @"Adam";
NSPredicate *predicate   = [NSPredicate predicateWithFormat:@"%K like %@",
        attributeName, attributeValue];

解析结果为: firstName like “Adam” .

单/双引号的变量(或替代变量的字符串)%@,%K,或$变量被解释为一个文本格式字符串,从而防止任何替换。在下面的例子中,解析结果为: firstName like “%@” (注意%@是单引号的)。

NSString *attributeName = @"firstName";
NSString *attributeValue = @"Adam";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like '%@'",
        attributeName, attributeValue];

注意: 不能用一个%@代替整个 predicate format 表达式(entire predicate), 如下面的例子不能通过编译:

NSString *expression = @"firstName like Adam";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@", expression];

比较运算(Basic Comparisons)

=, ==
BETWEEN

以下例子的表达式相当于 ( betweenPredicate >=1 && betweenPredicate<= 10 )

NSPredicate *betweenPredicate =
    [NSPredicate predicateWithFormat: @"attributeName BETWEEN %@", @[@1, @10]];
NSDictionary *dictionary = @{ @"attributeName" : @5 };
BOOL between = [betweenPredicate evaluateWithObject:dictionary];
if (between) {
    NSLog(@"between");

布尔predicate

TRUEPREDICATE 解析为 true
FALSEPREDICATE 解析为 false

NSArray *numberArray = @[@1, @4, @5, @20];
NSPredicate *p;
p = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"];
NSLog(@"All numbers: %@", [numberArray filteredArrayUsingPredicate:p]);
p = [NSPredicate predicateWithFormat:@"FALSEPREDICATE"];
NSLog(@"No numbers: %@", [numberArray filteredArrayUsingPredicate:p]);
/* printf:结果:
All numbers: (
No numbers: (

逻辑运算(Basic Compound Predicates)

与: AND, &&
或: OR, ||
非: NOT, !

NSPredicate *p = [NSPredicate predicateWithFormat:@"(age > 25) AND (name == %@), @"john"];

字符串计算(String Comparisons)

BEGINSWITH 开始
ENDSWITH 结尾
CONTAINS 包含
LIKE 模糊匹配,可用*和?通配符
MATCHES 正则表达式

以下2个和UTI(universal type identifier)有关,我不知道是什么用途,知道的网友告知我一下。
UTI-CONFORMS-TO
UTI-EQUALS

集合运算(Aggregate Operations)

ANY, SOME 匹配容器中至少有1个元素
ALL 匹配容器中所有元素
NONE 和ALL相反,容器中所有元素都不匹配。
注:以上3个为模糊匹配,IN为精确匹配。
IN 左边的表达式必须出现在右边的集合中,如:name IN { ‘Ben’, ‘Melissa’, ‘Nick’ }

以下4个数组运算不知道怎么使用:
array[index] 数组中特定元素
array[FIRST] 数组的第一个元素
array[LAST] 数组的最后一个元素
array[SIZE] 数组大小

+ predicateWithFormat:

//+ (NSPredicate *)predicateWithFormat:(NSString *)format, ...
NSArray *words = @[
                   @[@"aab", @"ccd"],
                   @[@"abc", @"def", @"g"],
                   @[@"test", @"t2"]
NSArray *words2 = @[@"aa", @"bb", @"cddd"];
###+ predicateWithFormat:###
//========筛选元素中包含特定字符串的数组?===========//
//模糊匹配
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat:@"ANY SELF CONTAINS %@", @"aa"];
NSLog(@"array1 = %@",[words filteredArrayUsingPredicate:predicate]);
//精确匹配
predicate = [NSPredicate predicateWithFormat:@"ANY SELF IN %@", @"aab"];
NSLog(@"array2 = %@",[words filteredArrayUsingPredicate:predicate]);
//多个匹配
predicate = [NSPredicate predicateWithFormat:@"ALL SELF IN %@", @[@"test", @"t2"]];
NSLog(@"array3 = %@",[words filteredArrayUsingPredicate:predicate]);
//不包含某字符的匹配
predicate = [NSPredicate predicateWithFormat:@"NONE SELF CONTAINS %@", @"aa"];
NSLog(@"array4 = %@",[words filteredArrayUsingPredicate:predicate]);
//断言(嵌套数组是精确匹配)
predicate = [NSPredicate predicateWithFormat:@"ANY %@ CONTAINS[cd] %@", words,@"a" ];
BOOL exsitW = [predicate evaluateWithObject:words];
NSLog(@"exsitW1 = %@",exsitW ? @"yes": @"no");
//断言(单个数组是模糊匹配)
predicate = [NSPredicate predicateWithFormat:@"ANY %@ CONTAINS %@", words[0],@"a" ];
exsitW = [predicate evaluateWithObject:words];
NSLog(@"exsitW2 = %@",exsitW ? @"yes": @"no");
2015-08-29 17:10:07.226 GVRegularExpressionDemo[9485:426418] array1 = (
2015-08-29 17:10:07.226 GVRegularExpressionDemo[9485:426418] array2 = (
2015-08-29 17:10:07.226 GVRegularExpressionDemo[9485:426418] array3 = (
        test,
2015-08-29 17:10:07.226 GVRegularExpressionDemo[9485:426418] array4 = (
        test,
2015-08-29 17:10:07.227 GVRegularExpressionDemo[9485:426418] exsitW1 = no
2015-08-29 17:10:07.227 GVRegularExpressionDemo[9485:426418] exsitW2 = yes

+ predicateWithFormat:argumentArray:

// + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat
//                       argumentArray:(NSArray *)arguments
NSArray *argArr = @[ @[@"h",@"i"] ];
predicate = [NSPredicate predicateWithFormat:@"ALL SELF IN %@" argumentArray:argArr];
NSLog(@"array5 = %@",[words filteredArrayUsingPredicate:predicate]);
/* printf:
2015-08-29 17:10:07.247 GVRegularExpressionDemo[9485:426418] array5 = (

+predicateWithFormat:arguments:

//+ (NSPredicate *)predicateWithFormat:(NSString *)format
//                           arguments:(va_list)argList
-(void)arg_list_test:(NSArray*)words andArgList:(NSString*)args, ...{
    va_list argList;
    va_start(argList, args);
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF CONTAINS %@" arguments:argList];
    va_end(argList);
    NSLog(@"array6 = %@",[words filteredArrayUsingPredicate:predicate]);
[self arg_list_test:words andArgList:@"a", @"c"];
/* printf:
2015-08-29 17:10:07.247 GVRegularExpressionDemo[9485:426418] array6 = (

- predicateWithSubstitutionVariables:

// - (instancetype)predicateWithSubstitutionVariables:(NSDictionary *)variables
predicate = [NSPredicate predicateWithFormat:@"ANY SELF IN $strVar"];
NSPredicate *subPre = [predicate predicateWithSubstitutionVariables:@{@"strVar":@"aab"}];
NSLog(@"array7 = %@",[words filteredArrayUsingPredicate:subPre]);
/* printf:
2015-08-29 17:10:07.247 GVRegularExpressionDemo[9485:426418] array7 = (

+ predicateWithValue:

// + (NSPredicate *)predicateWithValue:(BOOL)value
predicate = [NSPredicate predicateWithValue:YES];
NSLog(@"array8 = %@",[words filteredArrayUsingPredicate:predicate]);
/* printf:
2015-08-29 17:10:07.247 GVRegularExpressionDemo[9485:426418] array8 = (
        test,

+ predicateWithBlock:

//+ (NSPredicate *)predicateWithBlock:(BOOL (^)(id evaluatedObject,
//                             NSDictionary *bindings))block
predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    NSLog(@"evaluatedObject = %@, bindings = %@",evaluatedObject, bindings);
    if (bindings) {
        return [[evaluatedObject objectAtIndex:0] isEqualToString:[bindings valueForKey:@"value"]];
    }else{
        return [evaluatedObject isEqualToString:@"aa"];
NSLog(@"exsitW3 = %d", [predicate evaluateWithObject:words2 substitutionVariables:@{@"value":@"aa"}]);
NSLog(@"array9 = %@", [words2 filteredArrayUsingPredicate:predicate]);
2015-08-29 17:10:07.247 GVRegularExpressionDemo[9485:426418] evaluatedObject = (
), bindings = {
    value = aa;
2015-08-29 17:10:07.247 GVRegularExpressionDemo[9485:426418] exsitW3 = 1
2015-08-29 17:10:07.248 GVRegularExpressionDemo[9485:426418] evaluatedObject = aa, bindings = (null)
2015-08-29 17:10:07.248 GVRegularExpressionDemo[9485:426418] evaluatedObject = bb, bindings = (null)
2015-08-29 17:10:07.249 GVRegularExpressionDemo[9485:426418] evaluatedObject = cddd, bindings = (null)
2015-08-29 17:10:07.249 GVRegularExpressionDemo[9485:426418] array9 = (

Evaluating a Predicate

- evaluateWithObject:

//- (BOOL)evaluateWithObject:(id)object
//… 上面用得太多, 不写了.

- evaluateWithObject:substitutionVariables:

// - (BOOL)evaluateWithObject:(id)object
//     substitutionVariables:(NSDictionary *)variables
predicate = [NSPredicate predicateWithFormat:@"ANY SELF IN $strVar"];
BOOL exsitW4 = [predicate evaluateWithObject:words2 substitutionVariables:@{@"strVar":@"aaaaaaaaaaaaaaaaa"}];
NSLog(@"exsitW4 = %@", exsitW4 ? @"yes" : @"no");
/* printf:
exsitW4 = yes

- allowEvaluation

// - (void)allowEvaluation //不明白有什么用途 ?????, 知道的盆友告诉我一下.
[predicate allowEvaluation];  //不明白有什么用途

Getting a String Representation

predicateFormat Property

// @property(readonly, copy) NSString *predicateFormat
NSLog(@"predicate.predicateFormat = %@",predicate.predicateFormat);
/* printf:
predicate.predicateFormat = ANY SELF IN $strVar
                    PS: apple 称这个 format 为文本解释器(predicate string parser), 和正则表达式完全不是一个东西.空格和关键字大小写不敏感, 支持括号嵌套的表达式, 不做类型检查; $variable 表示变量, ?是非法关键字; 支持 printf 网络的格式说明符, 其中%k和%@很重要.%@ 对象值占位符, 一般是一个字符串, 数字, 日期等.
       NSPredicate类是用来定义逻辑条件约束的获取或内存中的过滤搜索。
      您可以使用谓词来表示逻辑条件,用于描述对象持久性存储在内存中的对象过滤。虽然从NSComparisonPredicate,
 NSCompoundPr
Predicate这个单词的英语含义是断言(假设)。它在Java中是一个接口,在java.util.function.Predicate包下。
Predicate类常用作集合类型的流的过滤条件。
import java.util.Arrays;
import java.util.List;
import java.util.function.IntPredicate;
import java.util.function.Predicate;
import java.util.stre
				
if (list != null && list.size() > 0){ Expression<Long> exp = root.<Long>get("xxx"); predicateList.add(exp.in(list));
private List<Predicate> getAndPredicateList(CriteriaBuilder criteriaBuilder, CriteriaQuery query,Root root, DangerInfoSearchDTO dto) { // 多条件And 查询 List<Predicate> predicateList =.
+ (BOOL) validateEmail:(NSString *)email NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MAT
这次我们来分享Predicate这个函数,还是按照以往的风格我们来进行对比的操作,话不多说,我们来切入正题—&amp;amp;amp;amp;gt; 将predicate里面的函数一一剖析。 ###test()方法 1.我们直接先来看Predicate这个函数的源码解释 1 . 代表着一个**“断定式子”** 2 . 这是一个实用的接口—&amp;amp;amp;amp;gt;其中的实用方法指的是test方法 **PS:下面就是te...
iOS的模糊查询使用的是NSPredicate来实现的,如果你的模糊查询失效了,可能是因为你的NSPredicate语句有误。以下是一些常见的NSPredicate模糊查询语句: 1. 使用LIKE操作符进行模糊匹配: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE[c] '*searchKeyword*'"]; 其中,name表示要查询的属性名,[c]表示不区分大小写,searchKeyword表示你要查询的关键词。 2. 使用CONTAINS操作符进行模糊匹配: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[c] 'searchKeyword'"]; 其中,name表示要查询的属性名,[c]表示不区分大小写,searchKeyword表示你要查询的关键词。 如果你的模糊查询还是失效了,你可以检查一下你的查询语句是否正确,或者你可以提供更多的信息给我,我会尽力帮助你解决问题。