Swift中如何按AccountNumber和FIPS合并NSArray与NSMutableArray?
实现NSArray与NSMutableArray的规则化合并
针对你提出的数组合并需求——通过AccountNumber和FIPS双字段匹配,将检查项合并到对应属性项的Inspectionlist子数组,未匹配项作为新元素添加,我整理了一套可直接复用的Objective-C实现方案:
核心实现思路
- 先给
NSMutableArray中的每个属性字典初始化空的Inspectionlist数组,避免后续添加元素时出现键不存在的问题 - 遍历
NSArray中的每一条检查项,用AccountNumber+FIPS作为匹配条件,在NSMutableArray中查找对应属性项 - 匹配成功:将检查项追加到对应属性项的
Inspectionlist中 - 匹配失败:将该检查项作为新字典直接添加到
NSMutableArray末尾 - 额外处理空值场景(比如AccountNumber为
<null>),避免空指针异常
完整代码示例
// 替换为你的实际数组对象 NSArray *inspectionsArray = @[ @{@"AccountNumber": @"G110080003900", @"City": @"JACKSON", @"FIPS": @"39079", @"InspectionId": @"425", @"InspectionLogId": @"366", @"InspectionType": @"OCA Inspector", @"InspectionTypeId": @"2", @"LandUseCode": @"1001", @"ScheduledDate": @"2018-05-21T00:00:00", @"State": @"OH", @"Status": @"Done", @"StatusId": @"2", @"StreetName": @"JISCO WEST", @"StreetNumber": @"4564", @"TemplateId": @"1075", @"TemplateName": @"AVM Inspection", @"UserId": @"51", @"UserPropertyId": @"1546", @"XCoordinate": @"39.03665", @"YCoordinate": @"-82.6797", @"Zip": @"45640"}, @{@"AccountNumber": @"000053809", @"City": @"0", @"FIPS": @"5051", @"InspectionId": @"427", @"InspectionLogId": @"368", @"InspectionType": @"OCA Inspector", @"InspectionTypeId": @"2", @"LandUseCode": @"8001", @"ScheduledDate": @"2018-05-22T00:00:00", @"State": @"AR", @"Status": @"Done", @"StatusId": @"2", @"StreetName": @"CYPRESSVALE", @"StreetNumber": @"0", @"TemplateId": @"1075", @"TemplateName": @"AVM Inspection", @"UserId": @"51", @"UserPropertyId": @"1547", @"XCoordinate": @"0", @"YCoordinate": @"0", @"Zip": @"0"}, @{@"AccountNumber": @"000053809", @"City": @"0", @"FIPS": @"5051", @"InspectionId": @"428", @"InspectionLogId": @"369", @"InspectionType": @"OCA Inspector", @"InspectionTypeId": @"2", @"LandUseCode": @"8001", @"ScheduledDate": @"2018-05-23T00:00:00", @"State": @"AR", @"Status": @"In Progress", @"StatusId": @"4", @"StreetName": @"CYPRESSVALE", @"StreetNumber": @"0", @"TemplateId": @"1075", @"TemplateName": @"AVM Inspection", @"UserId": @"51", @"UserPropertyId": @"1547", @"XCoordinate": @"0", @"YCoordinate": @"0", @"Zip": @"0"}, @{@"AccountNumber": @"633-342-004", @"City": @"INDIAN WELLS", @"FIPS": @"6065", @"InspectionId": @"349", @"InspectionLogId": @"290", @"InspectionType": @"OCA Inspector", @"InspectionTypeId": @"2", @"LandUseCode": @"1001", @"ScheduledDate": @"2018-03-19T00:00:00", @"State": @"CA", @"Status": @"Completed", @"StatusId": @"3", @"StreetName": @"CHEYENNE", @"StreetNumber": @"77431", @"TemplateId": @"1090", @"TemplateName": @"Casualty Loss Inspection", @"UserId": @"51", @"UserPropertyId": @"1482", @"XCoordinate": @"33.72453", @"YCoordinate": @"-116.313739", @"Zip": @"92210"} ]; NSMutableArray *propertiesMutableArray = [@[ @{@"AccountNumber": @"000053809", @"AssessmentRollId": @"<null>", @"AssessmentYear": @"0", @"BuildingArea": @"0", @"City": @"0", @"CreatedBy": @"51", @"DateCreated": @"2018-05-21T00:01:47.93", @"DateModified": @"2018-05-21T00:01:47.93", @"FIPS": @"5051", @"Id": @"1547", @"LandUseCode": @"8001", @"LotSizeSquareFeet": @"0", @"PropertyId": @"78781545", @"State": @"AR", @"StreetName": @"CYPRESSVALE", @"StreetNumber": @"0", @"TotalAssessedValue": @"600", @"UpdatedBy": @"51", @"UserId": @"51", @"XCoordinate": @"0", @"YCoordinate": @"0", @"YearBuilt": @"0", @"Zip": @"0"}, @{@"AccountNumber": @"<null>", @"AssessmentRollId": @"<null>", @"AssessmentYear": @"<null>", @"BuildingArea": @"<null>", @"City": @"Houston", @"CreatedBy": @"51", @"DateCreated": @"2018-05-22T01:14:43.29", @"DateModified": @"2018-05-22T01:14:43.29", @"FIPS": @"48201", @"Id": @"1548", @"LandUseCode": @"338", @"LotSizeSquareFeet": @"2525", @"PropertyId": @"1", @"State": @"TX", @"StreetName": @"san Felipe suite 2600", @"StreetNumber": @"5847", @"TotalAssessedValue": @"<null>", @"UpdatedBy": @"51", @"UserId": @"51", @"XCoordinate": @"29.749522", @"YCoordinate": @"-95.481959", @"YearBuilt": @"2013", @"Zip": @"77057"} ] mutableCopy]; // 步骤1:为每个属性字典初始化Inspectionlist数组 for (NSMutableDictionary *propertyDict in propertiesMutableArray) { if (!propertyDict[@"Inspectionlist"]) { propertyDict[@"Inspectionlist"] = [NSMutableArray array]; } } // 步骤2:遍历检查项,匹配并合并 for (NSDictionary *inspectionDict in inspectionsArray) { NSString *inspectionAccount = inspectionDict[@"AccountNumber"]; NSString *inspectionFIPS = inspectionDict[@"FIPS"]; BOOL isMatched = NO; for (NSMutableDictionary *propertyDict in propertiesMutableArray) { NSString *propertyAccount = propertyDict[@"AccountNumber"]; NSString *propertyFIPS = propertyDict[@"FIPS"]; // 处理空值的安全比较 BOOL accountMatch = ((inspectionAccount == propertyAccount) || ([inspectionAccount isEqualToString:propertyAccount])); BOOL fipsMatch = ((inspectionFIPS == propertyFIPS) || ([inspectionFIPS isEqualToString:propertyFIPS])); if (accountMatch && fipsMatch) { [(NSMutableArray *)propertyDict[@"Inspectionlist"] addObject:inspectionDict]; isMatched = YES; break; // 找到匹配项后跳出循环,提升效率 } } // 未匹配到任何属性项,添加为新元素 if (!isMatched) { [propertiesMutableArray addObject:inspectionDict]; } } // 输出合并后的结果 NSLog(@"合并完成后的数组:%@", propertiesMutableArray);
结果说明
用你提供的示例数据测试后,会得到如下符合预期的结果:
- 第一个属性字典(AccountNumber=000053809,FIPS=5051)的
Inspectionlist会包含2条匹配的检查项 - NSArray中剩下的2条未匹配检查项(G110080003900和633-342-004)会被添加到
propertiesMutableArray的末尾 - 原有的第二个属性字典(AccountNumber=
,FIPS=48201)保持不变, Inspectionlist为空数组
内容的提问来源于stack exchange,提问作者B.Saravana Kumar
相关产品推荐
相关产品推荐

