Swift 5:如何更新数组数组中的值?
Swift 5: How do I update values in an Array of arrays?
我正在尝试更新数组数组中的值,但我猜 for item in buffer
一定是在缓冲区中制作项目的副本,而不是提供对原始项目的引用。除了某种 for i=...{buffer[i][3]='Moved'}
之外,还有其他方法可以做到这一点吗?
var buffer = [[String]]()
let bufRemoved = buffer.filter({[=11=][3] == "Removal"})
let bufAdded = buffer.filter({[=11=][3] == "Addition"})
let moved = bufRemoved.filter({item in bufAdded.contains(where: {[=11=][2] == item[2]})})
for var item in buffer {
if moved.contains(where: {[=11=][2] == item[2]}) {
switch item[3] {
case "Removal":
item[3] = "Moved(out)"
case "Addition":
item[3] = "Moved(in)"
default:
break
}
}
}
let bufMoved = buffer.filter({[=11=][3].contains("Move")})
一个解决方案是enumerate
数组也有索引
for (index, item) in buffer.enumerated() {
if moved.contains(where: {[=10=][2] == item[2]}) {
switch item[3] {
case "Removal":
buffer[index][3] = "Moved(out)"
case "Addition":
buffer[index][3] = "Moved(in)"
default:
break
}
}
}
我正在尝试更新数组数组中的值,但我猜 for item in buffer
一定是在缓冲区中制作项目的副本,而不是提供对原始项目的引用。除了某种 for i=...{buffer[i][3]='Moved'}
之外,还有其他方法可以做到这一点吗?
var buffer = [[String]]()
let bufRemoved = buffer.filter({[=11=][3] == "Removal"})
let bufAdded = buffer.filter({[=11=][3] == "Addition"})
let moved = bufRemoved.filter({item in bufAdded.contains(where: {[=11=][2] == item[2]})})
for var item in buffer {
if moved.contains(where: {[=11=][2] == item[2]}) {
switch item[3] {
case "Removal":
item[3] = "Moved(out)"
case "Addition":
item[3] = "Moved(in)"
default:
break
}
}
}
let bufMoved = buffer.filter({[=11=][3].contains("Move")})
一个解决方案是enumerate
数组也有索引
for (index, item) in buffer.enumerated() {
if moved.contains(where: {[=10=][2] == item[2]}) {
switch item[3] {
case "Removal":
buffer[index][3] = "Moved(out)"
case "Addition":
buffer[index][3] = "Moved(in)"
default:
break
}
}
}