我有两个数组,一个代表全尺寸图像列表,另一个代表图像的缩略图。有没有办法使用 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);
}
}
关于ios - 使用 NSPredicate 比较两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15743326/