1

When I try to create an array of UIButtons in the ViewController

Instance member 'tile11' cannot be used on type 'ViewController'

It works when I create it inside a method, but I need the array in more than one method, and it's getting annoying to having to create the array again and again.

class ViewController: UIViewController {
    let tileArray: Array<UIButton> = [tile11, tile12, tile13, tile21, tile22, tile23, tile31, tile32, tile33]
}
0

1 Answer 1

4

There isn't enough code to tell for sure, but I'm guessing tile11 and the other tiles are other instance variables (button outlets to be specific)? They would technically not be accessible until run-time. This is why you cannot instantiate tileArray like that, but why it works in a method. The only way to instantiate tileArray in line with the declaration is to use constants in the array. You should initialize tileArray in viewDidLoad().

Sign up to request clarification or add additional context in comments.

3 Comments

Okay thanks for the clarification. They are outlets yes.
When moving it to the viewDidLoad() I can't access it in my methods.
Declare it as var tileArray: Array<UIButton> outside methods so that all can see it, but assign the value in viewDidLoad.

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.