[英]How to insert number in struct array (Swift4)
My code is not working. 我的代码不起作用。 All I am trying to do is get the number inserted into the struct's internal array. 我想要做的就是将数字插入到struct的内部数组中。 Right now this is not working: 现在这不起作用:
@IBAction func move(_ sender: Any) {
bad.numbers.insert(0, at: 0)
}
struct bad {
var numbers: [Int] = [1, 2, 3]
}
You need to declare your numbers property as static. 您需要将您的Numbers属性声明为static。 Btw it is Swift convention to name your structures starting with an uppercase letter: 顺便说一句,Swift惯例是以大写字母开头命名结构:
struct Bad {
static var numbers: [Int] = [1, 2, 3]
}
And to insert elements at index 0, you need to call it like this : 要在索引0处插入元素,您需要像这样调用它:
Bad.numbers.insert(0, at: 0)
print(Bad.numbers) // "[0, 1, 2, 3]\n"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.