1

I have a class (NavBar) with a couple variables that I want to place in an array. I am currently getting this error.

'Instance member buttonOne cannot be used on type NavBar'

The code throwing the error is as follows.

// Buttons
var buttonOne: Button?
var buttonTwo: Button?
var buttonThree: Button?
var buttonFour: Button?
var buttonFive: Button?
var buttonsArray: [Button] = [buttonOne, buttonTwo, buttonThree, buttonFour, buttonFive]
4
  • do at least what Arsen recommended. However, the error you posted barely relates to the optionality of your Button instances. Commented Oct 7, 2015 at 12:22
  • Your code is missing some context for this error to make complete sense. Commented Oct 7, 2015 at 12:33
  • try to upload missing context of your code. Commented Oct 7, 2015 at 12:36
  • @Abhinav below provided enough of the answer. I had to initiate the array empty first then later within a function I add the values to the array. Commented Oct 8, 2015 at 1:34

3 Answers 3

3

Well, error says it all, you cannot add instance variables into another instance variable (Array). What you are looking for is something like this:

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]
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is the only answer which correctly address (and explains) the actual error being asked about in the question.
@Abhinav I would really appreciate you marking this question as a good question if you feel that way.
1

You cannot store optional values in array of values. Use [Button?] instead of [Button]

var buttonsArray: [Button?] = [buttonOne, buttonTwo, buttonThree, buttonFour, buttonFive]

1 Comment

Assuming the variables in the question are all instance variables of a class, this will produce the same error. You're only solving a problem that's not being asked about.
0

Not sure what your base class is but you should get the idea. The following setup works:

class Button {}

class MyClass {

    var buttonOne: Button?
    var buttonTwo: Button?
    var buttonThree: Button?
    var buttonFour: Button?
    var buttonFive: Button?
    var buttonsArray: [Button?]

    init() {
        buttonsArray = [buttonOne, buttonTwo, buttonThree, buttonFour, buttonFive]
    }
}

Ad Arsen pointed out, the array should be of [Button?] type to handle optionals and the array itself should be constructed in your init method. Depending on your base class this can be initWithCoder: or some other initialiser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.