Cannot pass immutable value of type "NSLayoutConstraint" as inout argument

I am trying to reassign the NSLayoutConstraint link.

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 he gives me an error:

 exchangeConstraint(&myConstraint) -------------------^ Cannot pass immutable value of type 'NSLayoutConstraint' as inout argument 

I don’t understand why it says an immutable value, whereas a constraint is declared as a variable, not a constant.

+5
source share
1 answer

I solved this by simply declaring the constraint parameter as the apparently intact NSLayoutConstraint .

 func exchangeConstraint(_ constraint: inout NSLayoutConstraint!) { ... } 

UPDATE

Here is a project in which I use it: https://github.com/truffls/compatible-layout-anchors-ios

+1
source

Source: https://habr.com/ru/post/1273880/


All Articles