iOS - 当字符串以算术运算符+ - */开头时,NSPredicate string.length总是求值为0

iup*_*s10 4 core-data nspredicate

我有一个简单的方法,使用NSPredicate返回comments.length> 0的行数.

问题是,我发现当Comment列以+ - *或/开头时,length属性总是计算为0,因此该行被排除在计数之外.

我在SQLite浏览器中打开了表,并验证了该列是VARCHAR.使用SQLite查询检查字符串长度工作得很好(LENGTH(ZComments)> 0),所以这必须是CoreData问题.

这是我的功能......

-(int)getCommentsCount{
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    [request setIncludesSubentities:YES];
    [request setEntity:[NSEntityDescription entityForName:@"InspectionRecord" inManagedObjectContext:managedObjectContext]];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(comments.length > 0)"];
    [request setPredicate:predicate];

    NSError *err;
    NSUInteger count = [managedObjectContext countForFetchRequest:request error:&err];

    //count is ALWAYS 0 if 'comments' starts with + - * or /     WHYYY???      
    return count;
}
Run Code Online (Sandbox Code Playgroud)

我能够通过检查空/空字符串而不是使用.length来解决这个问题,但是我真的很想知道.当字符串以某些字符开头时,.length会失败.

这发生在任何人身上吗?

Mar*_*n R 7

您不能像length在Core Data fetch请求中那样使用Objective-C函数(当Core Data将获取请求转换为SQLite查询时,将忽略".length"部分).但您可以简单地与空字符串进行比较:

 [NSPredicate predicateWithFormat:@"comment != ''"]
Run Code Online (Sandbox Code Playgroud)

对于涉及长度的其他查询,可以使用带有正则表达式的MATCHES运算符,如下所示:CoreData谓词:字符串属性长度?.