核心数据 - NSPredicate过滤掉空字符串工作不正确

Won*_*jae 20 core-data ios

我一直在我的项目中使用核心数据.几天前,我发现保存在数据库中的一些记录没有显示在应用程序UI中.我已经跟踪了它,发现当我使用NSPredicate过滤掉空字符串时,它们根本没有被取出.所有这些都以非字母字符开头.

为了澄清这个问题,我创建了一个示例项目并将一些示例数据添加到数据库中.假设它们是"Sample","+ sample","Sample +".

这是我用来过滤掉空字符串的代码片段."text"是字符串属性的名称,而moc是NSManagedObjectContext实例.

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"BasicEntity"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"text.length > 0"];
[request setPredicate:predicate];

NSArray *samples = [moc executeFetchRequest:request error:&error];
Run Code Online (Sandbox Code Playgroud)

结果数组只包含2个实体,即"Sample"和"Sample +".

我甚至在包含上面示例字符串的简单数组上尝试了相同的谓词(当然,使用self.length而不是text.length),并且我正确地得到了所有3个.

我想知道是否有人遇到同样的问题.或者我在使用核心数据时遗漏了什么?在iOS 7.0.3模拟器和7.0.6 iPad Air上测试.


更新:正如在另一个线程中所回答的,我使用正则表达式解决了这个问题.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"text MATCHES %@", @".{1,}"];
Run Code Online (Sandbox Code Playgroud)

不过,我相信我使用的原始谓词是有效的.我将向Apple提交一个错误以获取他们的意见.

Sri*_*tty -3

尝试这个:

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