使用SQLITE存储的Core Data NSPredicate

3

这段代码返回了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];

FullName属性包含Unicode数据(阿拉伯语)。

任何帮助都将不胜感激。

1个回答

7

尝试:

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

2
使用%@替换会自动将值解释为字符串,不需要在其周围添加单引号。如果您需要添加单引号,则不需要使用%K替换。 - Dave DeLong
@Dave,只是好奇%K格式说明符是做什么用的?我从未使用过它,并且在苹果字符串编程指南或IEEE PRINTF规范中也找不到任何文档。 - Kevin Sylvestre
3
这个用于替代非引号包含的字符串值,因此主要用于键路径(因此是“%K”)。 在内部,它变成了一个'NSKeyPathExpression'而不是一个'NSConstantValueExpression'(如果您使用任何其他替换格式,则会出现后者)。 '%K'是特定于'NSPredicate'格式字符串的。 我不认为我在其他任何地方都遇到过它们。 - Dave DeLong

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接