如何从一个数组中获取与另一个数组具有相同属性的对象? [英] How get objects from one array with same properties of other?

查看:85
本文介绍了如何从一个数组中获取与另一个数组具有相同属性的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:我有两个 NSMutableArray.一个@[1,2,3,4,5,6,7].其他有像

For example: I have two NSMutableArray. One @[1,2,3,4,5,6,7]. Other have objects like

@[
   @{@idObjectToSearch":1, @"name":@"aaaaa", @"surname": @"bbbbb"}, @{@idObjectToSearch":2, @"name":@"aaaaa", @"surname": @"ccccc"},
    ...
   @{@idObjectToSearch":100, @"name":@"aaaaa", @"surname": @"cccdcd"}
];

那么我如何才能更有效地从第二个数组中提取所需的对象?

So how I could extract needed objects from second array more effective way?

推荐答案

您需要在第二个数组中使用 NSPredicate.

You need to use NSPredicate with your second array.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"idObjectToSearch IN %@", firstArray];
//In above predicate instead of passing `1` you need to pass object from first array that you want.

NSArray *filterArray = [secondArray filteredArrayUsingPredicate:predicate];

//Now access Array objects
if (filterArray.count > 0) {
     NSLog(@"%@",filterArray);
}

这篇关于如何从一个数组中获取与另一个数组具有相同属性的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆