2013-04-01 75 views
0

我有兩個數組,一個代表全尺寸圖像的列表,另一個代表圖像的縮略圖。有一種方法,使用NSPredicate來檢查全尺寸圖像是否有縮略圖?比較兩個數組與NSPredicate

拇指被稱爲img {number} _thumb.jpg,而全尺寸圖像被稱爲img {number} .jpg。

+0

將爲你工作? –

+0

我應該怎麼做?這是問題 –

回答

1

使用數組,字符串和循環

NSArray *[email protected][@"img1_thumb.jpg",@"img2_thumb.jpg",@"img3_thumb.jpg",@"img4_thumb.jpg",@"img5_thumb.jpg",]; 
NSArray *[email protected][@"img1",@"img2",@"img3",@"img41",@"img5"]; 

BOOL isSame=YES; 
for (NSString *name in images) { 
    if (![thumbs containsObject:[NSString stringWithFormat:@"%@_thumb.jpg",name]]) { 
     isSame=NO; 
     NSLog(@"%@ doesn't has thumb image",name); 
     break; //if first not found is not good enough remove this break 
    } 
} 
NSLog(@"%@",[email protected]"All thumb has image":@"All thumb does not have image"); 

使用NSPredicate:

for (NSString *image in images) { 
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF like [c]%@",[NSString stringWithFormat:@"%@_thumb.jpg",image]]; 
    NSArray *filtered=[thumbs filteredArrayUsingPredicate:predicate]; 
    if (filtered.count==0) { 
     NSLog(@"%@ not found",image); 
    } 
} 
+0

我寧願使用不循環,但我認爲這是唯一的方法。謝謝:) –

+0

我想你可以使用'enumerateObjectsUsingBlock:'同時檢查數組以進一步加速它? – Jay

1

您可以使用indexesOfObjectsPassingTest:

NSArray *imageThumbs= [NSArray arrayWithObjects:@"img1_thumb.jpg",@"img2_thumb.jpg",@"img3_thumb.jpg",@"img4_thumb.jpg",nil]; 
    NSArray *images=[NSArray arrayWithObjects: @"img1",@"img2",@"img3",@"img4",@"img5",nil]; 
    for(NSString *image in images) 
    { 
     if ([imageThumbs HasPrefix:image]) { 
      NSLog(@"has thumbnail %@",image); 
     } 
    } 

@interface NSArray (fileterArrayUsingBlocks) 
-(BOOL)HasPrefix : (NSString *)path; 
@end 
@implementation NSArray (fileterArrayUsingBlocks) 

-(BOOL)HasPrefix : (NSString *)path 
{ 
    NSIndexSet *lIndexSet = [self indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) { 
     if ([obj hasPrefix:path]) { 
      *stop = YES; 
      return YES; 
     } else 
      return NO; 
    }]; 
    if (![lIndexSet count]) 
     return NO; 
    return YES; 
} 

@end 
0

我F你可以安排兩個陣列具有相同的值(即,img1.jpg和img1_thumb.jpg都表示爲「1」,但在不同的陣列),然後在不縮略圖圖像組是:

[[NSMutableSet setWithArray:images] minusSet:[NSSet setWithArray:thumbnails]] 
使用數組和字符串的