0

I am declaring a constraint inside a UIViewController class called Home. Here's what the code looks like:

class Home: UIViewController {

@IBOutlet weak var buttonUpgrade: UIButton!

var constraint = NSLayoutConstraint (item: buttonUpgrade, 
attribute: NSLayoutAttribute.Bottom, 
relatedBy: NSLayoutRelation.Equal, 
toItem: self.view, 
attribute: NSLayoutAttribute.Bottom, 
multiplier: 1, 
constant: 500)

...
}

However, I get this error: Home.type does not have a member named buttonUpgrade. Why?

You can download the project from here: https://www.dropbox.com/s/x7avclri0ijw3pd/DemoConstraints.zip?dl=0.

Cesare
  • 9,139
  • 16
  • 78
  • 130
  • 1
    The code needs to be inside a method. Did you not notice that you have the same code in viewDidLoad, and that one doesn't give you an error? – rdelmar Jan 24 '15 at 18:50
  • Yes, I did notice it. Thank you for commenting. It would be VERY useful if I could just set up a global variable for the `NSLayoutConstraint` variable `var constraint` to avoid any errors such as `Unable to simultaneously satisfy constraints`. In fact, if I try to declare another time `var constraint` and assign to its `constant` a new value I receive that error. Any help would be much appreciated. – Cesare Jan 24 '15 at 18:56
  • I'm not sure what you mean, but you can create a property at the top of the file for the constraint, var constraint: NSLayoutConstraint! . Then when you create the constraint in viewDidLoad (or anywhere else), just use self.constraint = ... (actually, you don't even need the "self" in Swift, but I still like to use it). – rdelmar Jan 24 '15 at 19:00
  • The problem is that the initialization of `constraint` depends on other properties of the same class. Possible duplicate of http://stackoverflow.com/questions/25854300/how-to-initialize-properties-that-depend-on-each-other, http://stackoverflow.com/questions/25582853/type-does-not-have-a-member, or http://stackoverflow.com/questions/25855137/viewcontrol-type-does-not-have-a-member-named (with different solutions). – Martin R Jan 24 '15 at 19:04
  • @rdelmar That's a nice idea! Thank you. But how do I tell the compiler that constraint has `attribute: NSLayoutAttribute.Bottom`? – Cesare Jan 24 '15 at 19:04
  • Looking at this question that I posted some years ago makes me cringe. – Cesare Sep 28 '18 at 08:31

1 Answers1

2

As already mentioned, you can't write such code outside a function. But what you can do is declaring your variable global(outside a function) like that:

var constraint:NSLayoutConstraint!

Then you can initialize it in for example your viewDidLoad-method which will be called once:

constraint = NSLayoutConstraint (item: buttonUpgrade, 
attribute: NSLayoutAttribute.Bottom, 
relatedBy: NSLayoutRelation.Equal, 
toItem: self.view, 
attribute: NSLayoutAttribute.Bottom, 
multiplier: 1, 
constant: 500)

After you did that, you can access the constraint variable from everywhere in your class. Just make sure that the variable is filled before using it.

Christian
  • 22,585
  • 9
  • 80
  • 106