我的数据格式如下
[ “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])")
始终在第二次迭代期间并出现错误:“致命错误:ArraySlice索引超出范围”。
不知道为什么......
提前谢谢!
答案 0 :(得分:2)
试试这样:
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)")
}
}
}
答案 1 :(得分:1)
该代码现在适用于Swift 2.2