我有两个数组,一个代表全尺寸图像列表,另一个代表图像的缩略图。有一种使用NSPredicate的方法来检查全尺寸图像是否具有缩略图?
拇指称为img {number} _thumb.jpg,全尺寸图像称为img {number} .jpg。
最佳答案
使用数组,字符串和循环:
NSArray *thumbs=@[@"img1_thumb.jpg",@"img2_thumb.jpg",@"img3_thumb.jpg",@"img4_thumb.jpg",@"img5_thumb.jpg",];
NSArray *images=@[@"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(@"%@",isSame?@"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);
}
}