I try to reassign an referecend NSLayoutConstraint.
class ViewController: UIViewController {
@IBOutlet weak var myConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
exchangeConstraint(&myConstraint)
}
}
extension UIViewController {
func exchangeConstraint(_ constraint: inout NSLayoutConstraint) {
let spacing = constraint.constant
view.removeConstraint(constraint)
constraint = view.topAnchor.constraint(equalTo: anotherView.topAnchor, constant: spacing)
view.addConstraint(constraint)
}
}
But here it gives me the error:
exchangeConstraint(&myConstraint)
-------------------^
Cannot pass immutable value of type 'NSLayoutConstraint' as inout argument
What i don't understand is why it says immutable value, whereas the constraint is declared as a variable, not a constant.
myConstraint = doSomething(myConstraint)
. I just don't like using the variable two times in a line for one thing. I hope for a cleaner way. – AerobicNSLayoutConstraint
is reference type anyway. Why do you pass it as aninout
parameter? – HeartsexchangeConstraint(&myConstraint!)
–myConstraint!
produces an l-value that can be used withinout
. – Anaphrodisiac