Objective C 总结(十二):NSPredicate
谓词定义了真值条件,对象通过谓词进行筛选,判断是否与条件相匹配。
@interface NSPredicate : NSObject <NSCoding, NSCopying> { void *_reserved; } // Parse predicateFormat and return an appropriate predicate + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat argumentArray:(NSArray *)arguments; + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat, ...; + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat arguments:(va_list)argList; + (NSPredicate *)predicateWithValue:(BOOL)value; // return predicates that always evaluate to true/false + (NSPredicate*)predicateWithBlock:(BOOL (^)(id evaluatedObject, NSDictionary *bindings))block; - (NSString *)predicateFormat; // returns the format string of the predicate - (NSPredicate *)predicateWithSubstitutionVariables:(NSDictionary *)variables; // substitute constant values for variables - (BOOL)evaluateWithObject:(id)object; // evaluate a predicate against a single object - (BOOL)evaluateWithObject:(id)object substitutionVariables:(NSDictionary *)bindings; // single pass evaluation substituting variables from the bindings dictionary for any variable expressions encountered @end @interface NSArray (NSPredicateSupport) - (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate; // evaluate a predicate against an array of objects and return a filtered array @end @interface NSMutableArray (NSPredicateSupport) - (void)filterUsingPredicate:(NSPredicate *)predicate; // evaluate a predicate against an array of objects and filter the mutable array directly @end @interface NSSet (NSPredicateSupport) - (NSSet *)filteredSetUsingPredicate:(NSPredicate *)predicate; // evaluate a predicate against a set of objects and return a filtered set @end @interface NSMutableSet (NSPredicateSupport) - (void)filterUsingPredicate:(NSPredicate *)predicate; // evaluate a predicate against a set of objects and filter the mutable set directly @end
格式说明
- [NSPredicate predicateWithFormat: @"firstName==%@", @"bob"];
- [NSPredicate predicateWithFormat: @"%K==%@", @"firstName",@"bob"];
- NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat: @"firstName==$FirstName"];
NSDictionary *varDictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"bob", "firstName", nil];
NSPredicate *predicate = [predicateTemplate predicateWithSubstitutionVariables: varDictionary]; - BOOL isMatch = [predicate evaluateWithObject: somePerson]; // 进行计算
运算符
>:, >=, <:, <=, ==, !=, &&, ||, !
predicate = [NSPredicate predicateWithFormat: @"firstName != 'bob'"];
BETWEEN, IN,
[NSPredicate predicateWithFormat: @"height BETWEEN (160, 200)"];
[NSPredicate predicateWithFormat: @"firstName in {'bob', 'john'}"];
BEGINSWITH, ENDSWITH, CONTAINS,字符串匹配也可以应用一些比较修饰符
[c]不区分大小写[d]不区分发音符号[cd]一起应用
[NSPredicate predicateWithFormat: @"firstName BEGINSWITH[cd] 'bo'"];
LIKE, ?匹配单字母,*匹配多字母,也可以应用[cd]修饰
[NSPredicate predicateWithFormat: @"firstName LIKE '*bo?'"];
SELF表示对象自身
分类:
Objective-C
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 后端思维之高并发处理方案
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析
· 想让你多爱自己一些的开源计时器