gpt4 book ai didi

swift - 在两个字典数组中查找重复键以使用新数组字典更新旧数组字典

转载 作者:行者123 更新时间:2023-11-28 10:14:26 25 4
gpt4 key购买 nike

我想用新词典更新已保存的词典。如果不存在值,则需要将新值附加到字典中。还使用字典的新值更新旧的现有值。

旧保存的字典数组:

 var savedDict = [
["id":"1","pic":"Alice.png","name":"Alice Smith","position":"Nurse"],
["id":"2","pic":"brad.png","name":"Brad Smith MD","position":"Primary Doctor"],
["id":"12","pic":"bob.png","name":"Bob Smith PhD","position":"Hospital Coordinator"],
]

新下载字典数组:

let newDict =   [
["id":"1","pic":"alice.png","name":"Alice Smith","position":"Nurse"],
["id":"2","pic":"brad.png","name":"Brad Smith MD","position":"Primary Doctor"],
["id":"3","pic":"user.png","name":"Dr. Quam","position":"Immunologist"],
["id":"4","pic":"jennifer.jpg","name":"Jennifer Johnson","position":"Case Manager"],
["id":"5","pic":"user.png","name":"John Banks MD","position":"Cardiologist"],
["id":"6","pic":"tammie.png","name":"Tammie Summers","position":"Case Manager"]
]

我想:
1.将“Alice.png”更新为“alice.png”,因为“id”= 1,
2.忽略“id”= 2,因为两个字典数组都相同,
3. 将 newDict 中的所有新条目附加到 savedDict

最终的命令应该是:

 upToDateDict = [
["id":"1","pic":"alice.png","name":"Alice Smith","position":"Nurse"],
["id":"2","pic":"brad.png","name":"Brad Smith MD","position":"Primary Doctor"],
["id":"12","pic":"bob.png","name":"Bob Smith PhD","position":"Hospital Coordinator"],
["id":"3","pic":"user.png","name":"Dr. Quam","position":"Immunologist"],
["id":"4","pic":"jennifer.jpg","name":"Jennifer Johnson","position":"Case Manager"],
["id":"5","pic":"user.png","name":"John Banks MD","position":"Cardiologist"],
["id":"6","pic":"tammie.png","name":"Tammie Summers","position":"Case Manager"]
]

到目前为止我试过这个:

 import UIKit
import Foundation

func arrayContains(array:[[String:String]], value:[String:String]) -> Bool {
for item in array {
if item == value {
return true
}
}
return false
}

var upToDateDict = savedDict//Array<Dictionary<String,String>>()

//Save all of the dictionaries from the 1st array (savedDict) that aren't in the 2nd array (newDict)
for item in newDict {
if !arrayContains(array: savedDict, value: item) {
upToDateDict.append(item)
}
}

print(upToDateDict)

//find duplicate keys and check if need to update saved dict with new dict

let key = "id"

for dict1 in savedDict {
if let value = dict1[key] {
for dict2 in newDict {
if dict2[key] == value { //if true duplicate "id" key's found

if dict1 != dict2 { //not duplicate for all keys, so update saved with new dictionary

print("found \(key):\(value) in both arrays")
print("dict1:\(dict1)")
print("dict2:\(dict2)")

savedDict[dict1] = dict2 //what I want to do but will not build

} else {}//all keys duplicate in dict1 and dict2

}
}
}
}

我只是想用新的覆盖这个字典所以我更新 ["id":"1","pic":"Alice.png"... 到 ["id":"1","pic": “alice.png”...但这不起作用:

 savedDict[dict1] = dict2 

最佳答案

你应该创造

class Employee {
var id: String
var pic: String
var name: String
var position: String

init(id: String, pic: String, name: String, position: String) {
self.id = id
self.pic = pic
self.name = name
self.position = position
}
}

那么你就会有

var oldEmployes: [Employee] = [Employee(id: "1", pic: "pic.png", name: "Name", position: "Position"), 
Employee(id: "2", pic: "pic2.png", name: "Name2", position: "Position2")]
var newEmployes: [Employee] = [Employee(id: "2", pic: "pic21.png", name: "Name21", position: "Position2"),
Employee(id: "3", pic: "pic3.png", name: "Name3", position: "Position3")]

要更新数组,请执行类似的操作(使用高级函数可以更直观,但我认为这样看起来很清楚)

for employee in newEmployes {
var isNew = true
for oldEmployee in oldEmployes {
if oldEmployee.id == employee.id {
oldEmployee.name = employee.name
oldEmployee.pic = employee.pic
oldEmployee.position = employee.position
isNew = false
}
}
if isNew {
oldEmployes.append(employee)
}
}

关于swift - 在两个字典数组中查找重复键以使用新数组字典更新旧数组字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43101357/

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