gpt4 book ai didi

iOS 如何合并两个 NSArrays 并快速删除基于唯一 ID 的重复项?

转载 作者:行者123 更新时间:2023-11-28 15:06:26 32 4
gpt4 key购买 nike

在我的项目中,我想合并两个数组并根据唯一 ID 删除重复项,并在 swift 中将第二个数组中缺少的对象添加为合并数组的子数组?

这里我给出了第一个 NSArray:

[{
AccountNumber = 134679852;
AssessmentRollId = 12;
AssessmentYear = 1995;
City = houston;
DateCreated = "2018-01-22T18:43:53.6";
DateModified = "2018-01-22T18:43:53.603";
FIPS = 40125;
Id = 1223;
LandUseCode = 40125;
LotSizeSquareFeet = 45;
PropertyId = 3177;
},
{
AccountNumber = 134679852;
AssessmentRollId = 12;
AssessmentYear = 1995;
City = houston;
DateCreated = "2018-01-22T18:47:23.94";
DateModified = "2018-01-22T18:47:23.943";
FIPS = 40125;
Id = 1224;
LandUseCode = 40125;
LotSizeSquareFeet = 45;
PropertyId = 3177;
}]

这是第二个数组:

[{
AccountNumber = 547427;
City = "Berlin";
FIPS = 48453;
InspectionId = 143;
InspectionLogId = 90;
InspectionType = "Property Owner";
InspectionTypeId = 1;
ScheduledDate = "2018-01-22T00:00:00";
TemplateId = 1110;
TemplateName = "Beta validation Jennish";
UserId = 3177;
UserPropertyId = 1225;
},
{
AccountNumber = R157509;
City = "Santa Fe";
FIPS = 48167;
InspectionId = 144;
InspectionLogId = 91;
InspectionType = "Property Owner";
InspectionTypeId = 1;
ScheduledDate = "2018-01-22T00:00:00";
TemplateId = 1111;
TemplateName = "beta validation 3";
UserId = 3177;
UserPropertyId = 1226;
}]

请帮助我找到解决方案,我的技术知识有点薄弱,因为现在只有我在学习..

我想将这种类型设为父子格式:

[{
AccountNumber = 134679852;
AssessmentRollId = 12;
AssessmentYear = 1995;
City = houston;
DateCreated = "2018-01-22T18:43:53.6";
DateModified = "2018-01-22T18:43:53.603";
FIPS = 40125;
Id = 1223;
LandUseCode = 40125;
LotSizeSquareFeet = 45;
PropertyId = 3177;
InspectionDetails:{
InspectionId = 143;
InspectionLogId = 90;
InspectionType = "Property Owner";
InspectionTypeId = 1;
ScheduledDate = "2018-01-22T00:00:00";
TemplateId = 1110;
TemplateName = "Beta validation Jennish";
UserId = 3177;
UserPropertyId = 1225;
}
}]

这是我尝试使用 Set.. 删除重复项。

let mergedArray:NSArray = Array1.addingObjects(from: Array2 as! [Any]) as NSArray
let mySet = NSSet(array : mergedArray as! [Any])
let uniquArray:NSArray = mySet.allObjects as NSArray

最佳答案

首先我希望你有一个字典数组(String:Any),它真的是你想要的字典与数组的合并,如果我错了请纠正我。如果您没有拥有子数据的要求,这将很容易。您可以为字典创建一个扩展并循环遍历数组中的每个字典项并合并它们,因为情况并非如此,我想出了一个想法来实现您想要的,下面是代码(没有太多错误处理,没有完成太多性能测试,我修改了你的测试数据并将 Accountnumber 作为唯一 ID,并在第二个数组数据中进行了编辑,以便数组之间存在唯一 ID)

import UIKit

class ViewController: UIViewController {

var firstArray:[[String: Any]]!
var secondArray:[[String: Any]]!

override func viewDidLoad() {
super.viewDidLoad()
prepareData()
mergeDictionaries()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


func prepareData(){
let acc1:[String: Any] = ["AccountNumber" :134679852,
"AssessmentRollId":12,
"AssessmentYear" :1995,
"City":"houston",
"DateCreated":"2018-01-22T18:43:53.6",
"DateModified":"2018-01-22T18:43:53.603",
"FIPS" :40125,
"Id":1223,
"LandUseCode":40125,
"LotSizeSquareFeet" :45,
"PropertyId" :3177]
let acc2:[String: Any] = ["AccountNumber" :134679854,
"AssessmentRollId":12,
"AssessmentYear" :1995,
"City":"houston",
"DateCreated":"2018-01-22T18:43:53.6",
"DateModified":"2018-01-22T18:43:53.603",
"FIPS" :40125,
"Id":1223,
"LandUseCode":40125,
"LotSizeSquareFeet" :45,
"PropertyId" :3177]

firstArray = [acc1, acc2]

/// Fill the second array
let acc3:[String: Any] = [ "AccountNumber" :134679852,
"City" :"Berlin",
"FIPS" :48453,
"InspectionId" :143,
"InspectionLogId" :90,
"InspectionType" :"Property Owner",
"InspectionTypeId" :1,
"ScheduledDate" :"2018-01-22T00:00:00",
"TemplateId" :1110,
"TemplateName" :"Beta validation Jennish",
"UserId":3177,
"UserPropertyId" :1225]

let acc4:[String: Any] = ["AccountNumber" :547427,
"City" :"Berlin",
"FIPS" :48453,
"InspectionId" :144,
"InspectionLogId" :91,
"InspectionType" :"Property Owner",
"InspectionTypeId" :1,
"ScheduledDate" :"2018-01-22T00:00:00",
"TemplateId" :1111,
"TemplateName" :"Beta validation Jennish",
"UserId":3178,
"UserPropertyId" :1226]
secondArray = [acc3, acc4]
}
// This method will loop through each dictionary data in the first array and find the corresponding dictionary in the second array and then find the difference of data as a new dictionary and then create a final merged disctionary with the new data as a new key, then this dictionary will be added to a new array which finally contains all the merged data
func mergeDictionaries(){
var mergedArray = [[String: Any]]()
for dict in firstArray{

var mergedDict = dict
let accNumber = mergedDict["AccountNumber"] as! Int

// Find the data from second array which is related to the one in the first array, assuming AccountNumber is unique
let filtered = secondArray.filter{ $0["AccountNumber"] as! Int == accNumber }

if filtered.count == 0{
continue
}
// Get the new data from the second dictionary
let missingData = mergedDict.diffData(dictionary: filtered[0])
print(missingData)
// Add the new data as a new dictionary to the merged dictionary
mergedDict.updateValue(missingData, forKey: "InspectionDetails")
print("Merged data is \(mergedDict)")
mergedArray.append(mergedDict)
}
}
}


extension Dictionary {
func diffData(
dictionary: Dictionary<Key, Value>)->[Key: Value] {
var dict = [Key: Value]()
for (key, value) in dictionary {
// Key already exists dont do anything, we want only new keys fro the second dictionary.
if ((self[key]) != nil){
continue
}
dict[key] = value
}
return dict
}
/// function to do simple merge of two dictionaries
mutating func merge(
dictionary: Dictionary<Key, Value>) {
for (key, value) in dictionary {
self[key] = value
}
}

}

合并后的字典将包含以下数据,我相信这就是你想要的,然后将这个字典添加到新的合并数组中

["AccountNumber": 134679852, 
"Id": 1223,
"InspectionDetails": ["InspectionType": "Property Owner",
"TemplateName": "Beta validation Jennish",
"UserId": 3177,
"TemplateId": 1110,
"InspectionId": 143,
"InspectionTypeId": 1,
"ScheduledDate": "2018-01-22T00:00:00",
"InspectionLogId": 90,
"UserPropertyId": 1225],
"City": "houston",
"AssessmentRollId": 12,
"LotSizeSquareFeet": 45,
"FIPS": 40125,
"DateModified": "2018-01-22T18:43:53.603",
"LandUseCode": 40125,
"DateCreated": "2018-01-22T18:43:53.6",
"PropertyId": 3177,
"AssessmentYear": 1995]

关于iOS 如何合并两个 NSArrays 并快速删除基于唯一 ID 的重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48394876/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com