Search code examples
arraysxcodeswiftxcode-storyboard

How to place IBOutlets in a two dimensional array in swift?


I have some IBOutlets:

@IBOutlet weak var r1c1: MyLabel!
@IBOutlet weak var r1c2: MyLabel!
@IBOutlet weak var r2c1: MyLabel!
@IBOutlet weak var r2c2: MyLabel!

I want to place them in a two dimensional array:

var grid: [[MyLabel]] = [[r1c1,r1c2],[r2c1,r2c2]]

But when I compile, Xcode gives the following error:

InterfaceController.type does not have a member named 'r1c1'

What is the problem ?


Solution

  • I think your code is just fine, but you have to put your array inside of local scope:

    func printFirst() {
        var grid: [[MyLabel]] = [[r1c1,r1c2],[r2c1,r2c2]]
        println(grid[0][0]).text // or stringValue
    }
    

    Update

    @IBOutlet weak var r1c1: MyLabel!
    @IBOutlet weak var r1c2: MyLabel!
    @IBOutlet weak var r2c1: MyLabel!
    @IBOutlet weak var r2c2: MyLabel!
    
    var grid = [[MyLabel]]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        grid = [[r1c1,r1c2],[r2c1,r2c2]] 
    
    }