ArraySlice指数超出范围斯威夫特 [英] ArraySlice index out of range in Swift
问题描述
我有以下格式的数据。
[\"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","","DATA3-1","DATA3-2","DATA3-3","DATA3-4",""].
我需要在阵列的每个组件中执行特定的操作。的组件由,分离。换句话说,我首先需要执行操作[DATA1-1,DATA1-2,DATA1-3,DATA1-4],然后在[DATA2-1相同的操作, DATA2-2,DATA2-3,DATA2-4]等。
I need to perform specific operations within each component of the array. The components are separated by "". In other words, I first need to perform an operation on ["DATA1-1","DATA1-2","DATA1-3","DATA1-4"], then the same operation on ["DATA2-1","DATA2-2","DATA2-3","DATA2-4"], etc.
首先,我切片阵列分为单独的段,然后在每个迭代段:
First, I slice the array into separate segments, then iterate through each segment:
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])")
永远是第二次迭代有错误时:致命错误:ArraySlice指数超出范围
always during the second iteration with an error: "fatal error: ArraySlice index out of range".
不知道为什么...
感谢你在前进!
推荐答案
尝试这样的:
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指数超出范围斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!