Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import Foundation
class Singleton: NSObject {
static let sharedInstance = Singleton()
@objc dynamic var aProperty = false
func updateDoesntWork() {
aProperty = !aProperty
func updateDoesWork() {
Singleton.sharedInstance.aProperty = !aProperty
class Observer: NSObject {
override init() {
Singleton.sharedInstance.addObserver(self, forKeyPath: #keyPath(Singleton.aProperty), options: [.new], context: nil)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
print("observeValue(forKeyPath: \(String(describing:keyPath)), of: \(String(describing:object)), change: \(String(describing:change)), context:\(String(describing:context)))")
let observer = Observer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment