问题描述
- NSPredicate字符段长度
-
从一个数组中过滤出另一个数组,代码片段:NSMutableArray *filteredArray = [ [NSMutableArray alloc] initWithCapacity:1]; [wordsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSString *currentWord = (NSString *)obj; if(([currentWord length]>=4 && [currentWord length]<=6) && [currentWord rangeOfString:@" "].location == NSNotFound) { [filteredArray addObject:currentWord]; } }];
代码实现的很成功,但是我想如果用 filteredArrayUsingPredicate: 是不是会完成的更好一点?
应该怎么用 NSPredicate来代替 [currentWord length]>=4 && [currentWord length]<=6) && [currentWord rangeOfString:@" "].location == NSNotFound ?
解决方案
NSPredicate *p = [NSPredicate predicateWithFormat:@"length >= 4 AND length <= 6 AND NOT SELF CONTAINS ' '"];
时间: 2024-10-31 11:44:06