Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for bug when observing optionals with KVO #8

Open
tkrajacic opened this issue Jul 31, 2018 · 0 comments
Open

Workaround for bug when observing optionals with KVO #8

tkrajacic opened this issue Jul 31, 2018 · 0 comments

Comments

@tkrajacic
Copy link

// TODO: change.newValue should never be `nil`, but when observing an optional property that's set to `nil`, then change.newValue is `nil` instead of `Optional(nil)`. This is the bug report for this: https://bugs.swift.org/browse/SR-6066
guard let newValue = change.newValue else { return }
onChange(newValue)

This can be worked around with some funny casting:

let newValue = change.newValue ?? (nil as Any? as! Value)
onChange(newValue)

Then we can also observe optionals correctly.

Thanks to the awesome help of Jordan Rose of the Swift team for providing the workaround!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant