The behavior of NSLayoutConstraint.deactivate has been changed ?

The following code works fine on iOS 12, but on iOS 13.4.1, the variable is freed and It will crash."

The behavior of NSLayoutConstraint.deactivate has been changed. Is it?

Remove weak or NSLayoutConstraint.deactivate, the problem is gone. Which is appropriate in this case?


class ViewController: UIViewController {
     @IBOutlet private weak var constraint: NSLayoutConstraint!
     override func viewDidLoad() {
          NSLayoutConstraint.deactivate([constraint])
          constraint.constant = 100
          NSLayoutConstraint.activate([constraint])
     }
}

Does it crash at line 6 ?

As far as I understood, deactivate removes the constraint. So, if weak, it is effectively wipe out ?


Should test for nil before activating.

It will crash at line 5, because the constraint become nil after NSLayoutConstraint.deactivate.

The above code worked fine until iOS 12.


As I understand it, NSLayoutConstraint.deactivate is supposed to have the same role as "constraint.isActive = nil" but the reference count of constraint is reduced and it is released.

I asked here to see if their behavior changed in iOS 13.


There are two solutions I think.

1. remove weak word at line 2

2. remove line 4 and 6.

The behavior of NSLayoutConstraint.deactivate has been changed ?
 
 
Q