0

xCode Version 9.4.1

Hi,

I don't understand the error message given by Xcode console (despite many topic on this like this one :Instance member cannot be used on type 'ViewController'). Furthermore, when I have used "lazy var" in my code for ingStackView the message disappears.

class ViewController: UIViewController {
var tabStackView = [UIStackView]()
var nbIngStackView = UIStepper()


var ingCompleted : UITextField = {
    let text = UITextField()
    text.placeholder = "Ingrédient"
    text.borderStyle = .roundedRect
    return text
}()
var ingweight : UITextField = {
    let text = UITextField()
    text.placeholder = "Poids"
    text.borderStyle = .roundedRect
    return text
}()
var ingprice : UITextField = {
    let text = UITextField()
    text.placeholder = "Prix"
    text.borderStyle = .roundedRect
    return text
}()



var ingStackView : UIStackView = {
    var stackView = UIStackView()
    stackView.backgroundColor = .red
    stackView.axis = .horizontal
    stackView.alignment = .fill
    stackView.distribution = .fill
    stackView.addArrangedSubview(ingCompleted)
    stackView.addArrangedSubview(ingweight)
    stackView.addArrangedSubview(ingprice)
    stackView.translatesAutoresizingMaskIntoConstraints = false
    return stackView ]()
    }

In front of :

    stackView.addArrangedSubview(ingCompleted)
    stackView.addArrangedSubview(ingweight)
    stackView.addArrangedSubview(ingprice)

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

Thanks for your help!

Harjot Singh
  • 535
  • 7
  • 24
Paul Lecomte
  • 141
  • 1
  • 10
  • Possible duplicate of [How to initialize properties that depend on each other](https://stackoverflow.com/questions/25854300/how-to-initialize-properties-that-depend-on-each-other) – Cristik Oct 07 '18 at 19:49

1 Answers1

0

The accepted answer to the link you have provided explains this issue pretty well, IMHO. Basically, in your example, you are using the three "previously" declared UITextFields in your declaration of ingStackView. However, these four variables are not necessarily determined in that order at runtime. Xcode gives this error because, for example, ingprice may be determined after ingStackView. This is just an example, there is no guarantee in the order.

When you declare each of the variables with lazy var, the error goes away is because according to Apple's Swift Documentation:

A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy modifier before its declaration.

This means that for lazy variables, all of them are determined at initialization, so you are guaranteed to have all of the four variable values.

Xcoder
  • 1,433
  • 3
  • 17
  • 37
  • Thanks for your replying. I've perfectly understood. Is it better to use lazy var or to put `ingStackView` in `viewdidload`? – Paul Lecomte Oct 07 '18 at 14:54
  • Declaring `lazy` variables can be a more advanced concept. Putting `ingStackView` in `viewDidLoad()` can be easier to understand. It's up to you :) – Xcoder Oct 07 '18 at 15:17