Trying to replace old fashioned objective-c way of observation to modern swift way of observation. However I cannot figure out the keypath.
//doesn't work
progress.observe(.userInfo[ProgressUserInfoKey.fileCompletedCountKey], changeHandler: {_,_ in
})
PS: You cannot observe fileCompletedCount because is a computed property.
Old code:
import Cocoa
private var viewControllerObservationContext = 0
class ViewController: NSViewController {
private var progress: Progress = {
let progress = Progress()
progress.kind = .file
progress.fileTotalCount = 20
return progress
}()
override func viewDidLoad() {
super.viewDidLoad()
let filesCompletedKeyPath = "userInfo.NSProgressFileCompletedCountKey"
progress.addObserver(self, forKeyPath: filesCompletedKeyPath, options: .new, context: &viewControllerObservationContext)
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.progress.fileCompletedCount = 10
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
guard context == &viewControllerObservationContext else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
print(object)
print(keyPath)
}
}