美文网首页
iOS开发之NSPredicate

iOS开发之NSPredicate

作者: Cyyyyyyyy | 来源:发表于2016-04-20 22:52 被阅读605次

Predicate常被翻译成“谓词”,它被用于描述一个对象的性质或者对象间的相互关系,比如“小明是程序员”这个句子中的“是程序员”就是一个谓词。
在Cocoa中,NSPredicate是可以根据对象的性质或者相互关系来进行逻辑判断的工具。

只看概念不太好理解,直接上代码:

NSPredicate *isRich = [NSPredicate predicateWithFormat:@"account.balance > 5000000"];

Person *zhangSan = [[Person alloc] initWithName:@"ZhangSan" andBalance:9999];
Person *bill = [[Person alloc] initWithName:@"Bill" andBalance:1000000000];

NSLog(@"%d", [isRich evaluateWithObject:zhangSan]);  // 输出0
NSLog(@"%d", [isRich evaluateWithObject:bill]);  // 输出1

创建NSPredicate对象

从上面的例子里可以看到,我们可以通过NSPredicate提供的类方法,用格式化字符串方便地创建NSPredicate对象。

Cocoa为格式化字符串提供了丰富的语法支持:
比较运算符:<、<=、>、>=、=、!=、BETWEEN

[NSPredicate predicateWithFormat:@"account.balance > 5000000"];
[NSPredicate predicateWithFormat:@"account.balance BETWEEN {100, 1000}"];

复合运算符:OR、AND和NOT

[NSPredicate predicateWithFormat:@"startDate <= %@ AND endDate >= %@", now, now];

字符串判断:BEGINSWITH、CONTAINS
、ENDSWITH、LIKE、MATCHES

[NSPredicate predicateWithFormat:@"name BEGINSWITH 'Zhang'" ];
[NSPredicate predicateWithFormat:@"name CONTAINS 'ang'" ];
// LIKE支持通配符,*代表任意个字符,?代表仅有一个字符
[NSPredicate predicateWithFormat:@"url LIKE 'https://*.taobao.com*'" ];
// MATCHES支持正则表达式
NSString *regex = @"[0-9]*"; 
[NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

集合运算:ANY、SOME、ALL、NONE、IN

NSArray *names = @[@"ZhangSan", @"LiSi", @"WangWu"];
NSPredicate *anyIsZhang = [NSPredicate predicateWithFormat:@"ANY SELF BEGINSWITH 'Zhang'" ];
NSLog(@"%d", [anyIsZhang evaluateWithObject:names]);

常量和占位符:%@、%k、SELF

// 在格式化字符串的语法中,常量必须用引号包围起来,而属性名字不用
// 比如在name BEGINSWITH 'Zhang'这个格式化字符串里,name为属性,Zhang为常量

// 上面的例子大都是把常量硬编码在字符串里,但是我们的实际开发中会更多的用到占位符
[NSPredicate predicateWithFormat:@"name BEGINSWITH %@", familyName];

// 需要注意的是,在%@作为占位符时,它会被自动加上引号
// "name BEGINSWITH %@" 会变成 "name BEGINSWITH 'familyName'"
// 所以%@只能作为常量的占位符。
// 当我们想动态的填入属性名的时候,我们就必须这样写:
[NSPredicate predicateWithFormat:@"%k BEGINSWITH %@", propertyName, familyName];

// 在苹果的官方文档里面有一个%@错误用法的示范,大家可以想一下这样用为什么不对:
predicate = [NSPredicate predicateWithFormat:@"SELF like %@*%@", prefix, suffix];
ok = [predicate evaluateWithObject:@"prefixxxxxxsuffix"];

除了上面提到的使用格式化字符串创建NSPredicate的方法之外,Cocoa提供了另外两种创建NSPredicate的方法。它们的语法相对复杂,在这里就不多赘述了。

NSPredicate对象的用法

NSPredicate的最基本用法,就是创建一个NSPredicate对象,把要判断的对象传递给它,使用evaluateWithObject:方法判断:

NSPredicate *isRich = [NSPredicate predicateWithFormat:@"account.balance > 5000000"];
Person *zhangSan = [[Person alloc] initWithName:@"ZhangSan" andBalance:9999];
NSLog(@"%d", [isRich evaluateWithObject:zhangSan]);  // 输出0

我们还可以用NSPredicate来作为集合的过滤器用,这种写法相当简洁优雅:

NSArray *names = @[@"ZhangSan", @"LiSi", @"WangWu"];
NSPredicate *containsN = [NSPredicate predicateWithFormat:@"SELF CONTAINS 'n'" ];
NSLog(@"%@", [names filteredArrayUsingPredicate:containsN]); // 输出["ZhangSan", "WangWu"]

参考资料

Predicate Programming Guide

相关文章

  • CoreData+多线程

    基础知识:Core Data入门 查询语句:[IOS开发]CoreData条件查询之NSPredicate应用_超...

  • iOS开发之NSPredicate

    Predicate常被翻译成“谓词”,它被用于描述一个对象的性质或者对象间的相互关系,比如“小明是程序员”这个句子...

  • IOS开发之NSPredicate的使用

    最近在看一个开源项目,里面用到了NSPredicate类,感觉Foundation提供的NSPredicate类及...

  • iOS之NSPredicate

    使用NSPredicate对NSArray进行过滤 大家好,我是亮亮。过滤数组是经常要做的事情,最原始的方法是使用...

  • iOS 正则表达式的使用方式

    在 iOS 开发中,有三种常用的正则表达式运用方式,为别为:NSPredicate、NSRegularExpres...

  • IOS NSPredicate 查询、搜索

    IOS NSPredicate 查询、搜索简述:Cocoa框架中的NSPredicate用于查询,原理和用法都类似...

  • iOS开发之定位

    iOS开发之定位 iOS开发之定位

  • IOS NSSortDescriptor排序功能(附Demo)

    前文实现IOS的NSPredicate(查询功能) 传送门NSPredicate查询功能 本文主要实现生序降序对数...

  • iOS学习之NSPredicate

    Cocoa框架中的NSPredicate类一般用来,筛选查询字符串的匹配,原理和用法类似于SQL语句,作用相当于数...

  • iOS开发之GCD并发队列

    iOS开发多线程之GCDiOS开发之GCD同步任务加强iOS开发之GCD串行队列iOS开发之GCD并发队列 03 ...

网友评论

      本文标题:iOS开发之NSPredicate

      本文链接:https://www.haomeiwen.com/subject/jrmalttx.html