预想中如果count变量会随之变化时的代码
for index in 0..<(tempList.count - 1) {
if tempList[index] == UInt8(0x7D) && tempList[index + 1] == UInt8(0x22) {
tempList.remove(at:index)
tempList.remove(at:index)
tempList.append(UInt8(0x02))
} else if tempList[index] == UInt8(0x7D) && tempList[index + 1] == UInt8(0x5D) {
tempList.remove(at: index)
tempList.remove(at: index)
tempList.append(UInt8(0x7D))
}
}
而实际应该的思路,再新建个数组,对该数组进行操作然后赋值给原数组
var tempListTmp:[UInt8] = []
var flag = true
for index in 0..<tempList.count - 1 {
if flag {
if tempList[index] == UInt8(0x7D) && tempList[index + 1] == UInt8(0x22) {
tempListTmp.append(UInt8(0x02))
flag = false
} else if tempList[index] == UInt8(0x7D) && tempList[index + 1] == UInt8(0x5D) {
tempListTmp.append(UInt8(0x7D))
flag = false
}else{
tempListTmp.append(tempList[index])
}
} else {
flag = true
}
}
tempListTmp.append(tempList[tempList.count - 1])
tempList = Array(tempListTmp)