小编iup*_*s10的帖子

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

我有一个简单的方法,使用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会失败.

这发生在任何人身上吗?

core-data nspredicate

4
推荐指数
1
解决办法
3258
查看次数

标签 统计

core-data ×1

nspredicate ×1