美文网首页
iOS 遍历两个数组相同与不同的数据

iOS 遍历两个数组相同与不同的数据

作者: 文子飞_ | 来源:发表于2021-02-01 11:43 被阅读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()
        }
    
    

    相关文章

      网友评论

          本文标题:iOS 遍历两个数组相同与不同的数据

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