美文网首页
ios中其实可以少用循环遍历

ios中其实可以少用循环遍历

作者: 你别管 | 来源:发表于2017-03-02 18:18 被阅读0次

我们以KK_Order为例,MyPerson有orderID属性。

创建模拟数据:

NSMutableArray* allArray = [NSMutableArray arrayWithCapacity:1];

for(intindex =0; index <10; index++) {

KK_Order* order = [[KK_Orderalloc]init];

order.orderID= [NSStringstringWithFormat:@"%d",index];

[allArray addObject:order];

}

NSMutableArray* subArray = [NSMutableArray arrayWithCapacity:1];

for(intindex =0; index <4; index++) {

KK_Order* order = [[KK_Orderalloc]init];

order.orderID= [NSStringstringWithFormat:@"%d",index];

[subArray addObject:order];

}

NSMutableSet *set1 = [NSMutableSetsetWithArray:allArray];

NSMutableSet *set2 = [NSMutableSetsetWithArray:subArray];

1:数组取并集

[set1 unionSet:set2];

2数组取并集

[set1 intersectSet:set2];

3.取差集

[set1 minusSet:set2];

谓词

新建一个字符串数组

NSArray* strArray = [NSArrayarrayWithObjects:@"1",@"2",@"5",nil];

取出allArray中orderID在strArray包含的对象。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(orderID in %@)", strArray];

NSArray * result=[NSArray arrayWithArray:[allArray filteredArrayUsingPredicate: predicate]];

相关文章

网友评论

      本文标题:ios中其实可以少用循环遍历

      本文链接:https://www.haomeiwen.com/subject/heekgttx.html