核心数据NSPredicate与SQLITE商店

Ash*_*raf 3 iphone core-data nspredicate nsfetchrequest

此代码返回0个不正确的对象.但是,删除谓词时,获取请求将返回所有对象.

NSError *error = nil;

NSEntityDescription *entityDescription = [NSEntityDescription                                              entityForName:@"Person"  inManagedObjectContext:[self managedObjectContext]];

NSPredicate * pr = [NSPredicate predicateWithFormat:@"%K beginswith '%@' ",
                    @"FullName", searchText];

//NSPredicate * pr = [NSPredicate predicateWithFormat:@"PersonID == %@", searchText]; Works fine


NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
 [request setPredicate:pr];
NSArray * arr = [[self managedObjectContext] executeFetchRequest:request error:&error];
Run Code Online (Sandbox Code Playgroud)

FullName属性包含unicode数据(阿拉伯语).

任何帮助表示赞赏.

Kev*_*tre 7

尝试:

NSPredicate * pr = [NSPredicate predicateWithFormat:@"FullName beginswith %@", searchText];
Run Code Online (Sandbox Code Playgroud)

  • 它用于替换非引用的字符串值,因此它主要用于键路径(因此为'%K`).在内部,它变成了一个`NSKeyPathExpression`而不是一个`NSConstantValueExpression`(如果你使用了任何其他的替换格式,那就是它).`%K`特定于`NSPredicate`格式字符串.我不认为我在其他地方遇到过它们. (3认同)
  • +1使用`%@`替换将自动将该值解释为字符串.它周围不需要单引号.如果你需要添加单引号,那么就不需要'%K`替换. (2认同)