Swift数组设置索引值不起作用 [英] Swift array set value of index not working
问题描述
我有一个方法(下面的内容),其中queue2只是一个[Int]。我打印了很多东西,看看是否一切都达到了一定程度。
I have a method (contents below) where queue2 is just an [Int]. I printed a lot of things to see if everything was working up to a point.
public func cool(item: Int) {
println(item)
println(back)
//queue2.insert(item, atIndex: back)
queue2[back] = item
println(queue2.description)
println("done")
}
问题是这在运行时失败了,我不知道为什么。 Apple文档说你可以使用这种表示法设置数组中任何索引的值,但它不起作用。如果我取消注释注释行并注释掉它下面的一行,一切运行正常,但它不提供我需要的功能。给出了什么?
The problem is this fails at runtime and I don't know why. Apple docs say you can set the value of any index in an array with this notation, but it doesn't work. If I uncomment the commented line and comment out the one below it, everything runs fine but it doesn't provide the functionality I need. What gives?
推荐答案
如果 queue2
为空,此行为非法无论什么 返回
是:
If queue2
is empty, this line is illegal no matter what back
is:
queue2[back] = item
您不能引用不存在的索引,并且为空数组没有索引(索引)。
You cannot refer to an index that doesn't exist, and an empty array has no indexes (indices).
这篇关于Swift数组设置索引值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!