ios - 使用 NSPredicate 比较两个数组

标签 ios image cocoa nspredicate

我有两个数组,一个代表全尺寸图像列表,另一个代表图像的缩略图。有没有办法使用 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/

相关文章:

ios - 为什么我的应用程序存档中有许多 UUID dsym?

ios - 如何在 iOS App Swift 4 中集成 Paypal 支付方式?

image - 在 MATLAB 绘图中创建分形图像,但绘图为空

javascript - 在返回的map()对象中设置图像

objective-c - 我在使用 NSUserDefaults 时收到 SIGABRT

ios - glPushMatrix() 和 glPopMatrix() 抛出 GL_INVALID_OPERATION

c++ - OpenGL - 重新绑定(bind)一个已经绑定(bind)的纹理

android - child (wySprite,x)已经附加到另一个 parent (wySprite,xx),跳过addChild

cocoa - 如何实现狮子风格的滑动和滑走动画

javascript - React Native 超大图片定位