Adding observer for KVO without pointers using Swift

18,698

Solution 1

Now that KVOContext is gone in Xcode 6 beta 3, you can do the following. Define a global (i.e. not a class property) like so:

let myContext = UnsafePointer<()>()

Add an observer:

observee.addObserver(observer, forKeyPath: …, options: nil, context: myContext)

In the observer:

override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: [NSObject : AnyObject]!, context: UnsafePointer<()>) {
    if context == myContext {
        …
    } else {
        super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
    }
}

Solution 2

There is now a technique officially recommended in the documentation, which is to create a private mutable variable and use its address as the context.

(Updated for Swift 3 on 2017-01-09)

// Set up non-zero-sized storage. We don't intend to mutate this variable,
// but it needs to be `var` so we can pass its address in as UnsafeMutablePointer.
private static var myContext = 0
// NOTE: `static` is not necessary if you want it to be a global variable

observee.addObserver(self, forKeyPath: …, options: [], context: &MyClass.myContext)

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
    if context == &myContext {
        …
    }
    else {
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
    }
}

Solution 3

Swift 4 - observing contentSize change on UITableViewController popover to fix incorrect size

I had been searching for an answer to change to a block based KVO because I was getting a swiftlint warning and it took me piecing quite a few different answers together to get to the right solution. Swiftlint warning:

Block Based KVO Violation: Prefer the new block based KVO API with keypaths when using Swift 3.2 or later. (block_based_kvo).

My use case was to present a popover controller attached to a button in a Nav bar in a view controller and then resize the popover once it's showing - otherwise it would be too big and not fitting the contents of the popover. The popover itself was a UITableViewController that contained static cells, and it was displayed via a Storyboard segue with style popover.

To setup the block based observer, you need the following code inside your popover UITableViewController:

// class level variable to store the statusObserver
private var statusObserver: NSKeyValueObservation?

// Create the observer inside viewWillAppear
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    statusObserver = tableView.observe(\UITableView.contentSize,
        changeHandler: { [ weak self ] (theTableView, _) in self?.popoverPresentationController?.presentedViewController.preferredContentSize = theTableView.contentSize
        })
}

// Don't forget to remove the observer when the popover is dismissed.
override func viewDidDisappear(_ animated: Bool) {
    if let observer = statusObserver {
        observer.invalidate()
        statusObserver = nil
    }

    super.viewDidDisappear(animated)
}

I didn't need the previous value when the observer was triggered, so left out the options: [.new, .old] when creating the observer.

Solution 4

Update for Swift 4

Context is not required for block-based observer function and existing #keyPath() syntax is replaced with smart keypath to achieve swift type safety.

class EventOvserverDemo {
var statusObserver:NSKeyValueObservation?
var objectToObserve:UIView?

func registerAddObserver() -> Void {
    statusObserver = objectToObserve?.observe(\UIView.tag, options: [.new, .old], changeHandler: {[weak self] (player, change) in
        if let tag = change.newValue {
            // observed changed value and do the task here on change.
        }
    })
}

func unregisterObserver() -> Void {
    if let sObserver = statusObserver {
        sObserver.invalidate()
        statusObserver = nil
    }
  }
}
Share:
18,698

Related videos on Youtube

Justin Moore
Author by

Justin Moore

Updated on June 10, 2022

Comments

  • Justin Moore
    Justin Moore about 2 years

    In Objective-C, I would normally use something like this:

    static NSString *kViewTransformChanged = @"view transform changed";
    // or
    static const void *kViewTransformChanged = &kViewTransformChanged;
    
    [clearContentView addObserver:self
                           forKeyPath:@"transform"
                              options:NSKeyValueObservingOptionNew
                              context:&kViewTransformChanged];
    

    I have two overloaded methods to choose from to add an observer for KVO with the only difference being the context argument:

     clearContentView.addObserver(observer: NSObject?, forKeyPath: String?, options: NSKeyValueObservingOptions, context: CMutableVoidPointer)
     clearContentView.addObserver(observer: NSObject?, forKeyPath: String?, options: NSKeyValueObservingOptions, kvoContext: KVOContext)
    

    With Swift not using pointers, I'm not sure how to dereference a pointer to use the first method.

    If I create my own KVOContext constant for use with the second method, I wind up with it asking for this:

    let test:KVOContext = KVOContext.fromVoidContext(context: CMutableVoidPointer)
    

    EDIT: What is the difference between CMutableVoidPointer and KVOContext? Can someone give me an example how how to use them both and when I would use one over the other?

    EDIT #2: A dev at Apple just posted this to the forums: KVOContext is going away; using a global reference as your context is the way to go right now.

    • Joseph Mark
      Joseph Mark about 10 years
      are you asking how to create a CMutableVoidPointer?
    • Justin Moore
      Justin Moore about 10 years
      I've edited my post to be more precise.
  • RamenChef
    RamenChef over 7 years
    Can you explain this complete example?
  • csiu
    csiu over 7 years
    UnsafePointer<()>() is a null pointer. Using a null pointer as the context does not give you a particularly reliably unique value. (I realize this is out of date nowadays — so is my answer — just thought this should be mentioned for completeness.)
  • malhal
    malhal almost 4 years
    The question is about the context API, which by the way supports subclassing where as the block-based does not.