Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding observer for KVO without pointers using Swift

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.

like image 987
Justin Moore Avatar asked Jun 12 '14 02:06

Justin Moore


3 Answers

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)
    }
}
like image 160
jtbandes Avatar answered Nov 19 '22 01:11

jtbandes


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)
    }
}
like image 16
Aderstedt Avatar answered Nov 19 '22 01:11

Aderstedt


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.

like image 5
N.W Avatar answered Nov 19 '22 01:11

N.W