CoreData NSPredicate使用(相当于SQL where语句)

NSError *error;

NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Tz_task_from" inManagedObjectContext:context];

NSFetchRequest *request = [[NSFetchRequest alloc]init];

[request setEntity:entityDesc];

[request setReturnsObjectsAsFaults:NO];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"taskfrom=%@",userid];

[request setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"taskend" ascending:NO];

NSArray*sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];

[request setSortDescriptors:sortDescriptors];

NSArray *objects = [context executeFetchRequest:request error:&error];

1、比较运算符>,<,==,>=,<=,!=

2、范围运算符:IN、BETWEEN

3、字符串本身:SELF

4、字符串相关:

(1)、CONTAINS包含某个字符串

@nameCONTAINS[cd] %@

(2)、BEGINSWITH以某个字符串开头

@nameBEGINSWITH[c] %@

(3)、ENDSWITH以某个字符串结束

@nameENDSWITH[d] %@

注:

[c]不区分大小写

[d]不区分发音符号即没有重音符号

[cd]既不区分大小写,也不区分发音符号。

5、通配符:LIKE

例:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"customername LIKE[cd] '*%@*'",_searchString]];

6、正则表达式:MATCHES

例:NSString *regex = @"^A.+e$";  //以A开头,e结尾

@"name MATCHES %@",regex

你可能感兴趣的:(CoreData NSPredicate使用(相当于SQL where语句))