title: Checking if a value is changed using KVO in Swift 3
body: <p>I would like to know when a set of properties of a Swift object changes.  Previously, I had implemented this in Objective-C, but I'm having some difficulty converting it to Swift.</p>
  
  <p>My previous Objective-C code is:</p>
  
  <pre><code>- (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
      if (![change[@"new"] isEqual:change[@"old"]])
          [self edit];
  }
  </code></pre>
  
  <p>My first pass at a Swift solution was:</p>
  
  <pre><code>override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
      if change?[.newKey] != change?[.oldKey] {    // Compiler: "Binary operator '!=' cannot be applied to two 'Any?' operands"
          edit()
      }
  }
  </code></pre>
  
  <p>However, the compiler complains: "Binary operator '!=' cannot be applied to two 'Any?' operands"</p>
  
  <p>My second attempt:</p>
  
  <pre><code>override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
      if let newValue = change?[.newKey] as? NSObject {
          if let oldValue = change?[.oldKey] as? NSObject {
              if !newValue.isEqual(oldValue) {
                  edit()
              }
          }
      }
  }
  </code></pre>
  
  <p>But, in thinking about this, I don't think this will work for primitives of swift objects such as Int which (I assume) do not inherit from NSObject and unlike the Objective-C version won't be boxed into NSNumber when placed into the change dictionary.</p>
  
  <p>So, the question is how do I do the seemingly easy task of determining if a value is actually being changed using the KVO in Swift3?</p>
  
  <p>Also, bonus question, how do I make use of the 'of object' variable?  It won't let me change the name and of course doesn't like variables with spaces in them.</p>