我用以下格式的数组填充了符合结构的元素:
Struct Domain {
var name: String
var areas: [String]
}
let myArray = [
Domain(name: "name1", areas: ["area1A", "area1B"]),
Domain(name: "name2", areas: ["area2A", "area2B", "area2C"]),
Domain(name: "name3", areas: ["area3A"])
]
我知道如何将新的Domain对象附加到myArray,但是如何为myArray中的现有Domain对象附加新的区域条目?
答案 0 :(得分:0)
将数组let更改为var
var myArray = [
Domain(name: "name1", areas: ["area1A", "area1B"]),
Domain(name: "name2", areas: ["area2A", "area2B", "area2C"]),
Domain(name: "name3", areas: ["area3A"])
]
找到要更改的域的索引
if let indexOfdomain = myArray.firstIndex(where: {$0.name == "name2"}) {
myArray[indexOfdomain] = Domain(name: "name2", areas: ["area3A"])
}
print(myArray)