-1

我想过滤掉空记录,但我无法删除没有数据但有一系列空间的记录..

我有一个带有属性 "name" 的实体 A。我想查询实体 A 的所有对象,这些对象具有属性名称的一些文本值而不是“空间系列”,换句话说,我需要将其合并到查询中 -->

[name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet].length!=0

但我所能做的就是检查字符串的长度,包括不正确的空格。我正在使用-->

     NSPredicate* predicate = [NSPredicate predicateWithFormat:@"name!=' ' or name.length!=0 "];

欢迎您提出建议。

4

2 回答 2

0

如果您尝试删除特定字符串属性仅包含空格的对象,请使用基于正则表达式的谓词,然后删除您找到的任何对象。

使用这样的谓词进行 fetch 会找到所有这些对象:

NSPredicate *reqPredicate = [NSPredicate predicateWithFormat:@"attributeName matches ' +'"];

找到后,使用-[NSManagedObjectContext deleteObject:]删除生成的对象。

于 2013-10-28T18:08:51.943 回答
0

您可以修剪字符串并检查字符串的长度,如下所示:

NSString *trimmedString = [aRecordString stringByTrimmingCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@" "]]];

if (trimmedString.lenght > 0) {

    // the string if valid or at least is not just a spaces string
}
else {

    // the string is not valid or was just a spaces string
}

因此,如果您想在谓词中应用此检查,您可以使用带有块的谓词:

    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {

        // do your checks here and return TRUE or FALSE if the ovject match your criteria
}];
于 2013-10-28T13:31:49.620 回答