相关疑难解决方法(0)

NSPredicate测试NULL和空字符串

我有一个NSArray并且需要过滤掉任何null的字符串,或者更确切地说,有''(空字符串).我怎么做?我试过做:

NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"(name!=nil)"]; 
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用.或许它确实如此,但有不同种类的空...

objective-c nspredicate ios

73
推荐指数
4
解决办法
5万
查看次数

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
查看次数

标签 统计

nspredicate ×2

core-data ×1

ios ×1

objective-c ×1