2 for 2012 for_2012 于 2013.08.22 13:38 提问

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

1个回答

Mr_me
Mr_me   2013.08.22 14:57
已采纳
NSPredicate *p = [NSPredicate predicateWithFormat:@"length >= 4 AND length <= 6 AND NOT SELF CONTAINS ' '"];
Csdn user default icon
上传中...
上传图片
插入图片