0

The documentation says [when .initial is used]: The change dictionary in the notification will always contain an newKey entry if new is also specified but will never contain an oldKey entry.

In block-based KVO observer receives change struct, where oldValue is used.

oldValue has wild type Optional<Optional<Any>>.

Any forms of direct comparison (with nil, .some(nil) etc) are failed because of "Any is not Comparable" error.

How can I check that the call of observer is initial?

UPDATE

Well.

I'm observing a value: Any from some attribute. Really value is constructed from binary data inside the attribute and has the type in which it was delivered, the real type is Bool? in this case. Observer is fired correctly, it is not the problem.

observe(\ViewController.attribute.value, options: [.initial, .new, .old]) { (_self, change) in
    guard change.newValue as? Bool != change.oldValue as? Bool // does not work in initial
    else { 
        return 
    }
    ...
}

In initial case I have

(lldb) po change
▿ NSKeyValueObservedChange<Optional<Any>>
  - kind : __C.NSKeyValueChange
  ▿ newValue : Optional<Optional<Any>>
    ▿ some : Optional<Any>
      - some : <null>
  ▿ oldValue : Optional<Optional<Any>>
    - some : nil
  - indexes : nil
  - isPrior : false

New value is nil (attribute.value == .none). Old value, in accordance with documentation, must be not contained. What should I do to ensure that it is not contained really?

Alexey
  • 79
  • 7

1 Answers1

0

It seems that I found:

guard
    case let oldValue? = change.oldValue, oldValue == nil ||
    (change.newValue as? Bool != oldValue as? Bool)
else
{
    return
}
Alexey
  • 79
  • 7