從一個數組中過濾出另一個數組,代碼片段:
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 ' '"];