Iphone 创建NSPredicate以查找长度为>;N
我有一个字符串数组(超过5k个元素),每个字符串长度可变。我可以使用NSPredicate来查找数组中的特定字符串(需要一点时间才能弄清楚)。现在我需要找到长度大于N的元素 我已经阅读了文档,长度似乎不是谓词编程指南中可用的函数之一 提前谢谢Iphone 创建NSPredicate以查找长度为>;N,iphone,core-data,Iphone,Core Data,我有一个字符串数组(超过5k个元素),每个字符串长度可变。我可以使用NSPredicate来查找数组中的特定字符串(需要一点时间才能弄清楚)。现在我需要找到长度大于N的元素 我已经阅读了文档,长度似乎不是谓词编程指南中可用的函数之一 提前谢谢 Imagmast我不熟悉NSPredicate,因此我无法为您提供使用它的解决方案,但您不能只使用一些简单的NSString方法: //This is the length of the string you want to check int thres
Imagmast我不熟悉NSPredicate,因此我无法为您提供使用它的解决方案,但您不能只使用一些简单的NSString方法:
//This is the length of the string you want to check
int threshold = 5;
//Iterates through all elements in the array
for(int index = 0; index < [stringArray count]; index++) {
//Checks if the length of the string stored at the current index is greater than N
if([[stringArray objectAtIndex:index] length] > threshold) {
//String length is greater than N
}
}
为此:
if([[stringArray objectAtIndex:index] length] > threshold && [[stringArray objectAtIndex:index] isKindOfClass[NSString class]])
这样做的目的是确保当前索引处的对象是NSString。如果不是,您将收到一个运行时错误
NSArray * words= [allLinedStrings filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > %d",N]];
N是您在问题中提到的整数。为什么这一点被否决?我认为用评论来解释你的反对票是很有礼貌的。我猜这是因为问题没有发布代码尝试?一个更具建设性的回答(尤其是经验分数相对较低的回答)是让他发布字符串匹配代码+除非有人提供一个很好的理由-1.谢谢。它起作用了。您能否共享指向其他谓词“函数”的链接?请在调用
length
之前检查该类。
NSArray * words= [allLinedStrings filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > %d",N]];