我的代码无效。我想要做的就是将数字插入到struct的内部数组中。现在这不起作用:
@IBAction func move(_ sender: Any) {
bad.numbers.insert(0, at: 0)
}
struct bad {
var numbers: [Int] = [1, 2, 3]
}
答案 0 :(得分:3)
您需要将您的Numbers属性声明为static。顺便说一句,Swift惯例是以大写字母命名结构:
struct Bad {
static var numbers: [Int] = [1, 2, 3]
}
要在索引0处插入元素,您需要像这样调用它:
Bad.numbers.insert(0, at: 0)
print(Bad.numbers) // "[0, 1, 2, 3]\n"