0

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 ?

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
  • The problem is not the 2D array, but that the initialization of `grid` depends on other properties of the same class. You have to move the initialization to a *method*, e.g. viewDidLoad(). – See also http://stackoverflow.com/questions/25854300/how-to-initialize-properties-that-depend-on-each-other for an alternative solution. – Martin R Mar 25 '15 at 08:21
  • @MartinR Can you explain how to use "lazy" as you suggested in the other answer ? I'm not sure how to do it since this is an array. – Rahul Iyer Mar 25 '15 at 08:40
  • I closed the question as a duplicate of http://stackoverflow.com/questions/25855137/viewcontrol-type-does-not-have-a-member-named because I think it is the same problem. My answer to that question was to initialize the property in viewDidLoad. I have added a link to an alternative solution, but I did not write the answer to that one. – I did not suggest anything but presented two different solutions and you can choose from one. – Martin R Mar 25 '15 at 08:45
  • @MartinR I think you misunderstood. you are more knowledgeable than me, and I'm asking you how to do it. My English is not so good. I tried doing lazy like in that answer but I don't understand the syntax since this is an array. Can you suggest an answer ? I can't figure it out. – Rahul Iyer Mar 25 '15 at 08:47
  • This answer http://stackoverflow.com/a/25856755/1187415 applied to your problem would be `lazy var grid: [[UILabel]] = { [[self.r1c1,self.r1c2],[self.r2c1,self.r2c2]] }()`. – But the (updated) answer below works as well (that's essentially what I suggested in my answer to the "duplicate"). – Martin R Mar 25 '15 at 08:57

1 Answers1

0

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]] 

}
Prontto
  • 1,671
  • 11
  • 21