iOS 遍历两个数组相同与不同的数据
2021-02-01 本文已影响0人
文子飞_
遍历两个数组相同与不同的数据
- (void)testEnumerate {
/**
* 1、遍历itemModels的元素
* localItemModels里不包含就添加,包含不添加
*/
for (ItemModel *model in itemModels) {
NSString *fileId = model.fileId;
__block BOOL isContains = NO;
[localItemModels enumerateObjectsUsingBlock:^(ItemModel * _Nonnull localModel, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *localFileId = localModel.fileId;
if (![NSString stingIsNull:localFileId] && [localFileId isEqualToString:fileId]) {
isContains = YES;
*stop = YES;
}
}];
if (isContains == NO) {
[localItemModels addObject:model];
}
}
/**
* 2、遍历serverModels和dbModels的元素
* 匹配orderId相等,修改状态
* 获取最新状态的serverModels:filterArrays
*/
__block NSMutableArray *filterArrays = [NSMutableArray array];
// 遍历serverModels
[serverModels enumerateObjectsUsingBlock:^(WSFInformationOrderModel *serverModel, NSUInteger idx, BOOL *stop) {
// 遍历dbModels
[dbModels enumerateObjectsUsingBlock:^(WSFInformationOrderDBModel *dbModel, NSUInteger idx, BOOL *stop) {
// 匹配orderId相等,修改状态
if ([serverModel.orderId isEqualToString:dbModel.order_id]) {
if (serverModel.beInterestedMasterNum > dbModel.interested_master_count) {
serverModel.newInterestedMaster = YES;
} else {
serverModel.newInterestedMaster = NO;
}
[filterArrays addObject:serverModel];
*stop = YES;;
}
}];
// 未匹配到,防止已匹配重复加入
if (![filterArrays containsObject:serverModel]) {
if (serverModel.beInterestedMasterNum > 0) {
serverModel.newInterestedMaster = YES;
}
[filterArrays addObject:serverModel];
}
}];
}
// 2、遍历serverModels和checkModels的元素
private func filterCheckModel(checkModels: [WSFServiceCompleteImageModel]) {
// 找到相同的元素,修改状态
for serverModel in serverModels {
// 还原状态
serverModel.editState = .editStateUnSelect
for checkModel in checkModels {
if checkModel.orderId == serverModel.orderId {
// 修改状态
serverModel.editState = .editStateSelected
continue
}
}
}
// 刷新UI
imageCollectionView.reloadData()
}