Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift - KVO - change.newValue and change.oldValue are nil

Tags:

swift

I'm using the new KeyValue Observing. I'm getting a call to my observer when the variable changes, but the change struct comes with newValue and oldValue both as nil, so it never gets to the //do something part:

let observer = foo.observe(\.bar, options: [.new, .old]) { obj, change in
   if change.kind == .setting && change.oldValue != change.newValue {
       // do something
   }
}

It used to work perfectly with the old way of doing KVO.

I made that code more simple as an example, here is a debugging of my actual code. You can see that the m object has the status var has been set to PREPARING but both change.oldValue and change.newValue are nil:

enter image description here

like image 789
Martin Massera Avatar asked Nov 09 '17 19:11

Martin Massera


1 Answers

This is a Swift bug which has been reported as https://bugs.swift.org/browse/SR-5872. A pull request which addresses this issue is being discussed on https://github.com/apple/swift/pull/20757 but is not yet merged as of March 2022.

like image 176
0xced Avatar answered Nov 23 '22 13:11

0xced