我有一个类 (NavBar),其中有几个我想放在数组中的变量。我目前遇到此错误。
'Instance member buttonOne cannot be used on type NavBar'
抛出错误的代码如下。
// Buttons
var buttonOne: Button?
var buttonTwo: Button?
var buttonThree: Button?
var buttonFour: Button?
var buttonFive: Button?
var buttonsArray: [Button] = [buttonOne, buttonTwo, buttonThree, buttonFour, buttonFive]
最佳答案
好吧,错误说明了一切,您不能将实例变量添加到另一个实例变量(数组)中。您正在寻找的是这样的东西:
class navBar {
var buttonOne: Button?
var buttonTwo: Button?
var buttonThree: Button?
var buttonFour: Button?
var buttonFive: Button?
var buttonsArray : [Button?] = []
func addValues() {
buttonOne = Button()
buttonTwo = Button()
buttonThree = Button()
buttonFour = Button()
buttonFive = Button()
buttonsArray = [buttonOne, buttonTwo, buttonThree, buttonFour, buttonFive]
}
}
关于arrays - Swift 2.0 数组初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32991799/