Checking if a value is changed using KVO in Swift 3 - swift

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.
My previous Objective-C code is:
- (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
if (![change[#"new"] isEqual:change[#"old"]])
[self edit];
}
My first pass at a Swift solution was:
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()
}
}
However, the compiler complains: "Binary operator '!=' cannot be applied to two 'Any?' operands"
My second attempt:
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()
}
}
}
}
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.
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?
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.

Below is my original Swift 3 answer, but Swift 4 simplifies the process, eliminating the need for any casting. For example, if you are observing the Int property called bar of the foo object:
class Foo: NSObject {
#objc dynamic var bar: Int = 42
}
class ViewController: UIViewController {
let foo = Foo()
var token: NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
token = foo.observe(\.bar, options: [.new, .old]) { [weak self] object, change in
if change.oldValue != change.newValue {
self?.edit()
}
}
}
func edit() { ... }
}
Note, this closure based approach:
Gets you out of needing to implement a separate observeValue method;
Eliminates the need for specifying a context and checking that context; and
The change.newValue and change.oldValue are properly typed, eliminating the need for manual casting. If the property was an optional, you may have to safely unwrap them, but no casting is needed.
The only thing you need to be careful about is making sure your closure doesn't introduce a strong reference cycle (hence the use of [weak self] pattern).
My original Swift 3 answer is below.
You said:
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.
Actually, if you look at those values, if the observed property is an Int, it does come through the dictionary as a NSNumber.
So, you can either stay in the NSObject world:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let newValue = change?[.newKey] as? NSObject,
let oldValue = change?[.oldKey] as? NSObject,
!newValue.isEqual(oldValue) {
edit()
}
}
Or use them as NSNumber:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let newValue = change?[.newKey] as? NSNumber,
let oldValue = change?[.oldKey] as? NSNumber,
newValue.intValue != oldValue.intValue {
edit()
}
}
Or, I'd if this was an Int value of some dynamic property of some Swift class, I'd go ahead and cast them as Int:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let newValue = change?[.newKey] as? Int, let oldValue = change?[.oldKey] as? Int, newValue != oldValue {
edit()
}
}
You asked:
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.
The of is the external label for this parameter (used when if you were calling this method; in this case, the OS calls this for us, so we don't use this external label short of in the method signature). The object is the internal label (used within the method itself). Swift has had the capability for external and internal labels for parameters for a while, but it's only been truly embraced in the API as of Swift 3.
In terms of when you use this change parameter, you use it if you're observing the properties of more than one object, and if these objects need different handling on the KVO, e.g.:
foo.addObserver(self, forKeyPath: #keyPath(Foo.bar), options: [.new, .old], context: &observerContext)
baz.addObserver(self, forKeyPath: #keyPath(Foo.qux), options: [.new, .old], context: &observerContext)
And then:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard context == &observerContext else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
if (object as? Foo) == foo {
// handle `foo` related notifications here
}
if (object as? Baz) == baz {
// handle `baz` related notifications here
}
}
As an aside, I'd generally recommend using the context, e.g., have a private var:
private var observerContext = 0
And then add the observer using that context:
foo.addObserver(self, forKeyPath: #keyPath(Foo.bar), options: [.new, .old], context: &observerContext)
And then have my observeValue make sure it was its context, and not one established by its superclass:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard context == &observerContext else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
if let newValue = change?[.newKey] as? Int, let oldValue = change?[.oldKey] as? Int, newValue != oldValue {
edit()
}
}

My original 'second attempt' contains a bug that will fail to detect when a value changes to or from nil. The first attempt can actually be fixed once one realizes that all values of 'change' are NSObject:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
let oldValue: NSObject? = change?[.oldKey] as? NSObject
let newValue: NSObject? = change?[.newKey] as? NSObject
if newValue != oldValue {
edit()
}
}
or a more concise version if you prefer:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if change?[.newKey] as? NSObject != change?[.oldKey] as? NSObject {
edit()
}
}

Related

How to get Swift KVO working for static member?

I have a UIViewController with the following code. I want to know when the value of portrait effect is changed (in control center). I have tried AVCaptureDevice.isPortraitEffectEnabled and .portraitEffectEnabled, both have the same result: observeValue() is never called. I have verified that the value itself does actually change, and the docs state that KVO is supported for this member.
What am I missing?
To test this I am toggling the value of portaitEffectEnabled by calling AVCaptureDevice.showSystemUserInterface(.videoEffects) and turning it on/off, and expecting the KVO to fire.
#objc class EventSettingsCaptureViewController : UIViewController, ... {
required init(...) {
super.init(nibName: nil, bundle: nil)
if #available(iOS 15.0, *) {
AVCaptureDevice.self.addObserver(self, forKeyPath: "portraitEffectEnabled", options: [.new], context: nil)
}
}
deinit {
if #available(iOS 15.0, *) {
AVCaptureDevice.self.removeObserver(self, forKeyPath: "portraitEffectEnabled", context: nil)
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
// Breakpoint set here: never hits
if keyPath == "portraitEffectEnabled" {
guard let object = object as? AVCaptureDevice.Type else { return }
if #available(iOS 15.0, *) {
WLog("isPortraitEffectEnabled changed: \(object.isPortraitEffectEnabled)")
}
} else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
That won’t work because the AVCaptureDevice class itself doesn’t have a portraitEffectSupported property.
The issue is that the portraitEffectSupported property is an instance property.
you can always use class_copyPropertyList to double check that the property you’re trying to observe actually exists on that object. Here's an example:
import AVFoundation
func getPropertyNames(of target: AnyObject) -> [String] {
let itsClass: AnyClass = object_getClass(target)!
var count = UInt32()
guard let p = class_copyPropertyList(itsClass, &count) else {
return []
}
defer { p.deallocate() }
let properties = UnsafeBufferPointer(start: p, count: Int(count))
return properties.map { String(cString: property_getName($0)) }
}
// `AVCaptureDevice` has no class properties.
let propertiesOfTheClassItself = getPropertyNames(of: AVCaptureDevice.self)
print(propertiesOfTheClassItself) // => []
// Instances of `AVCaptureDevice` have some instance properties.
let propertiesOfASampleInstance = getPropertyNames(of: AVCaptureDevice.default(for: .video)!)
print(propertiesOfASampleInstance) // => ["transportControlsSupported", "transportControlsPlaybackMode", "transportControlsSpeed", "adjustingFocus", "adjustingExposure", "adjustingWhiteBalance"]

Swift 3 KVO to observe change in NSMutableSet (add, remove, modify item)

I'm develop an app which has an set of unique string. I have a function to add, remove, modify item in the NSMutableSet. I want to use KVO (key value observer) to observe whenever the set has change (add, remove, modify item).
Here's my code:
dynamic var barCodeSet = NSMutableSet()
in viewDidload I add observe:
override func viewDidLoad() {
super.viewDidLoad()
addObserver(self, forKeyPath: #keyPath(barCodeSet), options: [.old,.new,.initial], context: nil)
}
And this is my observe function:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(barCodeSet) {
print(barCodeSet.count)
for barcode in barCodeSet {
print(barcode)
}
}
}
I don't know why the KVO is not working. How can I modify the code so that we can get notify when set items change?
Assuming you do not need to use NSMutableSet, you could use a didSet clause in the variable declaration. try the following in a playground:
import UIKit
class myClass {
init() {
}
dynamic var barCodeSet: Set<String> = Set<String>() {
didSet {
print(barCodeSet.count)
for barcode in barCodeSet {
print(barcode)
}
}
}
}
let thisClass = myClass()
thisClass.barCodeSet = ["Apples", "Bananas", "Oranges"]
thisClass.barCodeSet.insert("Grapes")
Whenever you set the value of barCodeSet, its count and contents are printed to the console.

KVO and Swift 3.0: How to evaluate the change dictionary?

I am trying to figure out how to evaluate the [NSKeyValueChangeKey : AnyObject] change dictionary parameter in func observeValue(forKeyPath.... I have the following code in a playground and the way I'm evaluating the change dictionary I always end up thinking the change is a NSKeyValueChange.setting (which is definitely wrong).
What is the right way to evaluate the change dictionary?
import Foundation
class KVOTester: NSObject {
dynamic var items = [Int]() // Observe via KVO
override init() {
super.init()
self.addObserver(self, forKeyPath: #keyPath(KVOTester.items), options: [], context: nil)
}
deinit {
self.removeObserver(self, forKeyPath: #keyPath(KVOTester.items))
}
func exerciseKVO() {
self.items = [Int]() // NSKeyValueChange.setting
self.items.append(1) // NSKeyValueChange.insertion
self.items[0] = 2 // NSKeyValueChange.replacement
self.items.remove(at: 0) // NSKeyValueChange.removal
}
override func observeValue(forKeyPath keyPath: String?, of object: AnyObject?, change: [NSKeyValueChangeKey : AnyObject]?, context: UnsafeMutablePointer<Void>?) {
// We are only interested in changes to our items array
guard keyPath == "items" else { return }
// #1: object is the KVOTester instance - why isn't it the items array?
// #2 I don't understand how to use the change dictionary to determine what type of change occurred. The following
// is wrong - it *always* prints "Setting".
if let changeKindValue = change?[.kindKey] as? UInt, changeType = NSKeyValueChange(rawValue: changeKindValue) {
switch changeType {
case .setting:
print("Setting")
break
case .insertion:
print("Insertion")
break
case .removal:
print("Removal")
break
case .replacement:
print("Replacement")
break
}
}
}
}
let kvoTester = KVOTester()
kvoTester.exerciseKVO()
As the original questioner pointed out in his comment, the following code will give the expected result:
import Foundation
class KVOTester: NSObject {
dynamic var items = [Int]() // Observe via KVO
override init() {
super.init()
self.addObserver(self, forKeyPath: #keyPath(KVOTester.items), options: [], context: nil)
}
deinit {
self.removeObserver(self, forKeyPath: #keyPath(KVOTester.items))
}
func exerciseKVO() {
let kvoArray = self.mutableArrayValue(forKey: #keyPath(KVOTester.items))
items = [Int]() // NSKeyValueChange.setting
kvoArray.add(1) // NSKeyValueChange.insertion
kvoArray.replaceObject(at: 0, with: 2) // NSKeyValueChange.replacement
kvoArray.removeObject(at: 0) // NSKeyValueChange.removal
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
// We are only interested in changes to our items array
guard keyPath == "items" else { return }
if let changeKindValue = change?[.kindKey] as? UInt,
let changeType = NSKeyValueChange(rawValue: changeKindValue) {
switch changeType {
case .setting:
print("Setting")
break
case .insertion:
print("Insertion")
break
case .removal:
print("Removal")
break
case .replacement:
print("Replacement")
break
}
}
}
}
let kvoTester = KVOTester()
kvoTester.exerciseKVO()

Using switch in observeValueForKeyPath

I'm attempting to increase legibility of my KVO observeValueForKeyPath implementation by replacing the typical long string of nested if/else statements with a single switch statement.
So far, the only thing that's actually worked is:
private let application = UIApplication.sharedApplication()
switch (object!, keyPath!) {
case let (object, "delegate") where object as? UIApplication === application:
appDelegate = application.delegate
break
...
default:
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
Which, if anything, is even harder to read than:
if object as? UIApplication === application && keyPath! == "delegate" {
}
else {
}
Does anybody have a good model for using switch in observeValueForKeyPath (and similar methods)
EDIT: Relevant to #critik's question below, here's more of the code to demonstrate the problems with just using switch (object as! NSObject, keyPath!) {:
private let application = UIApplication.sharedApplication()
private var appDelegate : UIApplicationDelegate?
private var rootWindow : UIWindow?
public override func observeValueForKeyPath(
keyPath: String?,
ofObject object: AnyObject?,
change: [String : AnyObject]?,
context: UnsafeMutablePointer<Void>) {
switch (object as! NSObject, keyPath!) {
case (application, "delegate"):
appDelegate = application.delegate
(appDelegate as? NSObject)?.addObserver(self, forKeyPath: "window", options: [.Initial], context: nil)
break
case (appDelegate, "window"):
rootWindow = appDelegate?.window?.flatMap { $0 }
break
case (rootWindow, "rootViewController"):
rebuildViewControllerList(rootWindow?.rootViewController)
break
default:
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
How about a switch on tuples:
switch (object as! NSObject, keyPath!) {
case (application, "delegate"):
appDelegate = application.delegate
...
default:
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
Note 1. Although I'm agains forced stuff in Swift (downcasts, unwraps, etc), you can safely force downcast to NSObject without getting a crash, as per this SO question, KVO is only available for NSObject subclasses.
Note 2. You also don't need a break in Swift, this will also shorten your code by at least one line :)
This doesn't really solve the problem of using switch in observeValueForKey but it does demonstrate how I simplified the problem space to eliminate the verbose code.
I created a utility class, KVOValueWatcher which allows me to add (and remove) KVO observation on a single property of an object:
public class KVOValueWatcher<ObjectType:NSObject, ValueType:NSObject> : NSObject {
public typealias OnValueChanged = (ValueType?, [String:AnyObject]?) -> ()
let object : ObjectType
let keyPath : String
let options : NSKeyValueObservingOptions
let onValueChanged : OnValueChanged
var engaged = false
public init(object:ObjectType, keyPath:String, options : NSKeyValueObservingOptions = [], onValueChanged: OnValueChanged) {
self.object = object
self.keyPath = keyPath
self.onValueChanged = onValueChanged
self.options = options
super.init()
engage()
}
deinit {
if(engaged) {
print("KVOValueWatcher deleted without being disengaged")
print(" object: \(object)")
print(" keyPath: \(keyPath)")
}
disengage()
}
public func engage() {
if !engaged {
self.object.addObserver(self, forKeyPath: keyPath, options: options, context: nil)
engaged = true
}
}
public func disengage() {
if engaged {
self.object.removeObserver(self, forKeyPath: keyPath)
engaged = false
}
}
override public func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
self.onValueChanged(((object as? NSObject)?.valueForKeyPath(keyPath!) as? ValueType), change)
}
}
My problem code then becomes:
rootWindowWatcher = KVOValueWatcher(object: applicationDelegate as! NSObject, keyPath: "window", options: [.Initial]) { window, changes in
self.rootViewWatcher?.disengage()
self.rootViewWatcher = nil
if let window = window {
self.rootViewWatcher = KVOValueWatcher(object: window, keyPath: "rootViewController", options: [.Initial]) {
[unowned self] rootViewController, changes in
self.rootViewController = rootViewController
self.rebuildActiveChildWatchers()
}
}
}
The primary reason for the change was because it was becoming a nightmare to maintain all the different observations and get them properly added and removed. Adding the wrapper class eliminates the problem and groups watching a property and the action to be taken when the property changes in the same location.

KVO: How to get old / new values in observeValue(forKeyPath:...) in Swift?

I have created an observer with .Old | .New options. In the handler method I try to fetch before after values, but compiler complains: 'NSString' is not convertible to 'NSDictionaryIndex: NSObject, AnyObject
override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: [NSObject : AnyObject]!, context: UnsafeMutablePointer<Void>) {
let approvedOld = change[NSKeyValueChangeOldKey] as Bool
let approvedNew = change[NSKeyValueChangeNewKey] as Bool
iOS 11 and Swift >4.1
iOS 11 and Swift 4 brings significant changes to KVO.
The classes should adopt #objcMembers annotation in order to enable the KVO or KVO fails silently.
The variable to be observed must be declared dynamic.
Here is newer implementation,
#objcMembers
class Approval: NSObject {
dynamic var approved: Bool = false
let ApprovalObservingContext = UnsafeMutableRawPointer(bitPattern: 1)
override init() {
super.init()
addObserver(self,
forKeyPath: #keyPath(approved),
options: [.new, .old],
context: ApprovalObservingContext)
}
override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?) {
guard let observingContext = context,
observingContext == ApprovalObservingContext else {
super.observeValue(forKeyPath: keyPath,
of: object,
change: change,
context: context)
return
}
guard let change = change else {
return
}
if let oldValue = change[.oldKey] {
print("Old value \(oldValue)")
}
if let newValue = change[.newKey] {
print("New value \(newValue)")
}
}
deinit {
removeObserver(self, forKeyPath: #keyPath(approved))
}
}
There is also new bock based api for KVO, which works like this,
#objcMembers
class Approval: NSObject {
dynamic var approved: Bool = false
var approvalObserver: NSKeyValueObservation!
override init() {
super.init()
approvalObserver = observe(\.approved, options: [.new, .old]) { _, change in
if let newValue = change.newValue {
print("New value is \(newValue)")
}
if let oldValue = change.oldValue {
print("Old value is \(oldValue)")
}
}
}
}
Block based api look super good and easy to use. Also, KeyValueObservation is invalidated when deinited, so there is no hard requirement for removing observer.
Swift 2.0 and iOS < 10
With Swift 2.0, here is a complete implementation for a class that uses KVO,
class Approval: NSObject {
dynamic var approved: Bool = false
let ApprovalObservingContext = UnsafeMutablePointer<Int>(bitPattern: 1)
override init() {
super.init()
addObserver(self, forKeyPath: "approved", options: [.Old, .New], context: ApprovalObservingContext)
}
override func observeValueForKeyPath(keyPath: String?,
ofObject object: AnyObject?,
change: [String : AnyObject]?,
context: UnsafeMutablePointer<Void>) {
if let theChange = change as? [String: Bool] {
if let approvedOld = theChange[NSKeyValueChangeOldKey] {
print("Old value \(approvedOld)")
}
if let approvedNew = theChange[NSKeyValueChangeNewKey]{
print("New value \(approvedNew)")
}
return
}
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
deinit {
removeObserver(self, forKeyPath: "approved")
}
}
let a = Approval()
a.approved = true