gpt4 book ai didi

ArraySlice 索引在 Swift 中超出范围

转载 作者:搜寻专家 更新时间:2023-11-01 06:43:13 25 4
gpt4 key购买 nike

我有以下格式的数据

["DATA1-1","DATA1-2","DATA1-3","DATA1-4","","DATA2-1","DATA2-2","DATA2-3","DATA2-4","","DATA3-1","DATA3-2","DATA3-3","DATA3-4",""]。

我需要在数组的每个组件中执行特定操作。组件以“”分隔。换句话说,我首先需要对 ["DATA1-1","DATA1-2","DATA1-3","DATA1-4"] 执行一个操作,然后对 ["DATA2-1", "DATA2-2","DATA2-3","DATA2-4"],等

首先,我将数组分割成单独的段,然后遍历每个段:

func arraySliceFunction(airway: [String])
{
// Slice the master array with all combinations of airways into separate arrays
var airwaySlices = airway.split("")

// Iterate through the slices of arrays
for i in 0..<airwaySlices.count
{
let sliceComponent = airwaySlices[i]

// Iterate through each slice
for var y = 0; y < sliceComponent.count; y++
{
print("SLICES COMPONENT: \(sliceComponent[y])")
}
}
}

它在这一行崩溃了

print("SLICES COMPONENT: \(sliceComponent[y])")

总是在第二次迭代期间出现错误:“ fatal error :ArraySlice 索引超出范围”。

不知道为什么...

提前致谢!

最佳答案

像这样尝试:

func arraySliceFunction(array: [String]) {
// Slice the master array with all combinations of airways into separate arrays
// Iterate through the each subArray
for subArray in array.split("") {
// Iterate through each item in subArray
for item in subArray {
print("SLICES COMPONENT: \(item)")
}
}
}

关于ArraySlice 索引在 Swift 中超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33557914/

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