-1

As practice, I am trying to create a simple math app in which one can input two values as the bases of a right triangle and when they press a button the 3rd leg is calculated. This is the code I am using:

class pythagoreanTheoremViewController: UIViewController {
    @IBOutlet weak var baseOneTextField: UITextField!
    @IBOutlet weak var baseTwoTextField: UITextField!
    @IBOutlet weak var answerLabel: UILabel!

    var a = baseOneTextField.text.toInt()
}

However, this returns the error that pythagoreanTheoremViewController.Type does not have a member named 'baseOneTextField'. I deleted and then reconnected my outlets and I still have the same problem.

What am I doing wrong and how do I fix it?

Thanks

matt
  • 515,959
  • 87
  • 875
  • 1,141
jbcd13
  • 145
  • 8

1 Answers1

1

The problem is that your var a declaration is positioned at the top level of your class, so it is an instance variable. But an instance variable's initialization cannot refer to another instance variable (baseOneTextField), because the object is not yet formed at initialization time - forming the object is exactly what we are in the middle of doing.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I tried the lazy var workaround you posted in response to the other question but I still have the same error. – jbcd13 Jul 02 '15 at 17:24
  • Actually it's a different error. _Two_ different errors. You need to say `self.baseOneTextField.text!.toInt()` - notice the `self` and the exclamation mark. – matt Jul 02 '15 at 17:38
  • I must admit that I do not see why an obvious duplicate must be answered at all. There seem to be different opinions on meta, but as I understand http://meta.stackoverflow.com/questions/286283/should-users-be-permitted-to-both-answer-and-close-a-question, answering and then immediately dupe-hammer-closing is frowned upon. – Martin R Jul 02 '15 at 17:43
  • @MartinR I agree, actually. – matt Jul 02 '15 at 17:48