NSPredicate是获取和筛选数据的重要类,系统的集合类(NSArray, NSDictionary, NSSet等)和Coredata都提供了重要的API供开发者去使用,非常方便和实用。话不多说,直接上例子。
定义一个作者类
@interface Author : NSObject
{
NSString *name; //名称
NSInteger level; //等级
NSInteger bookNumber; //出版的数目
}
- (instancetype)initWithName:(NSString *)aName level:(NSInteger)aLevel bookNumber:(int)aBookNumber;
@end<pre name="code" class="objc">@implementation Author
- (instancetype)initWithName:(NSString *)aName level:(NSInteger)aLevel bookNumber:(int)aBookNumber
{
self = [super init];
if (self) {
name = aName;
level = aLevel;
bookNumber = aBookNumber;
}
return self;
}
@end
定义一个书类:
@interface Book : NSObject
{
Author *author; //作者
NSString *name; //书名
CGFloat price; //价格
}
- (instancetype)initWithAuthorName:(NSString *)aName authorLevel:(NSInteger)aLevel authorBookNumber:(int)aBookNumber bookName:(NSString *)bName bookPrice:(CGFloat)bPrice;
@end<pre name="code" class="objc">@implementation Book
- (instancetype)initWithAuthorName:(NSString *)aName authorLevel:(NSInteger)aLevel authorBookNumber:(int)aBookNumber bookName:(NSString *)bName bookPrice:(CGFloat)bPrice {
self = [super init];
if (self) {
author = [[Author alloc] initWithName:aName level:aLevel bookNumber:aBookNumber];
name = bName;
price = bPrice;
}
return self;
}
//初始化数据
Book *bookOne = [[Book alloc] initWithAuthorName:@"张三" authorLevel:1 authorBookNumber:100 bookName:@"iOS开发" bookPrice:89];
Book *bookTwo = [[Book alloc] initWithAuthorName:@"李四" authorLevel:1 authorBookNumber:95 bookName:@"Android开发" bookPrice:79];
Book *bookThree = [[Book alloc] initWithAuthorName:@"王五" authorLevel:2 authorBookNumber:82 bookName:@"java开发" bookPrice:69];
Book *bookFour = [[Book alloc] initWithAuthorName:@"赵六" authorLevel:3 authorBookNumber:70 bookName:@"PHP开发" bookPrice:59];
Book *bookFive = [[Book alloc] initWithAuthorName:@"麻子" authorLevel:4 authorBookNumber:55 bookName:@"HTML开发" bookPrice:49];
NSArray *_authorArray = @[bookOne,bookTwo,bookThree,bookFour,bookFive];
//基本格式
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@",@"author.name",@"张三"]; //bookOne
//1.== 或者 = (还能转换数据格式)
predicate = [NSPredicate predicateWithFormat:@"%K = %@",@"author.bookNumber", @100]; //bookOne
//2. >=
predicate = [NSPredicate predicateWithFormat:@"author.level >= 2"]; //bookThree,bookFour,bookFive
//3. <=
predicate = [NSPredicate predicateWithFormat:@"price <= 50"];
//4. >
predicate = [NSPredicate predicateWithFormat:@"author.bookNumber > 90"];//bookOne,bookTwo
//5. <
predicate = [NSPredicate predicateWithFormat:@"author.level < 2"];//bookOne,bookTwo
//6. != 或者 <>
predicate = [NSPredicate predicateWithFormat:@"price <> 69"];//bookOne,bookTwo,bookFour,bookFive
//7.BETWEEN 包含两端的数字,顺序没有影响
predicate = [NSPredicate predicateWithFormat:@"price BETWEEN {100,89}"];
//8.&&
predicate = [NSPredicate predicateWithFormat:@"%K = %d && %K > %d",@"author.level",1,@"author.bookNumber",95]; //bookOne
//9.||
predicate = [NSPredicate predicateWithFormat:@"%K = %d || %K > %d",@"author.level",1,@"author.bookNumber",80]; //bookOne,bookTwo,bookThree
//10.!
predicate = [NSPredicate predicateWithFormat:@"!(author.level = 1)"]; //bookThree,bookFour,bookFive
//11.BEGINSWITH
predicate = [NSPredicate predicateWithFormat:@"%K BEGINSWITH %@",@"name",@"iOS"];//bookOne
//12.ENDSWITH
predicate = [NSPredicate predicateWithFormat:@"%K ENDSWITH %@",@"author.name",@"子"]; //bookFive
//13.CONTAINS
predicate = [NSPredicate predicateWithFormat:@"name CONTAINS '开发'"]; //bookOne,bookTwo,bookThree,bookFour,bookFive
//14.LIKE
predicate = [NSPredicate predicateWithFormat:@"name LIKE 'i*发'"]; //bookOne
//15.ANY SOME
predicate = [NSPredicate predicateWithFormat:@"ALL name CONTAINS '开发'"];
//16. IN
predicate = [NSPredicate predicateWithFormat:@"author.name IN {'张三','李四'}"];//bookOne,bookTwo
筛选:
[bookArray filteredArrayUsingPredicate:predicate]
单个的对象也可以用NSPredicate进行筛选
例如筛选一个NSString是否是一个网页:
-(BOOL)isUrlString
{
BOOL checkResult = !self.isEmptyString;
if (checkResult) {
NSString *urlRegex = @"((http|HTTP|ftp|FTP)?://)?[0-9a-zA-Z/.-_]+(:[0-9]{1,4})?[0-9a-zA-Z/.-_]*";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegex];
if (![urlTest evaluateWithObject:self]) {
checkResult = NO;
}
}
return checkResult;
}