Observing a value of a static var in a class?

As for now, Swift cannot have observable class properties. (In fact, static properties are just global variables with its namespace confined in a class.)

If you want to use KVO, create a shared instance (singleton class) which has online property and add observer to the instance.


I solved it with the singleton pattern suggested by @OOper.

class ConnectionManager: NSObject {
    static let sharedInstance = ConnectionManager()
    private override init() {} // This prevents others from using the default '()' initializer for this class.
    @objc dynamic var online = false
}

Then:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.tableFooterView = UIView()

    ConnectionManager.sharedInstance.addObserver(self,
                         forKeyPath: "online",
                         options: [.new, .initial],
                         context: nil)
}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if object is ConnectionManager && keyPath == "online" {
        // ...
    }
}

Try replacing dynamic static var online = false to @nonobjc static var online = false

What's happening is that because it inherits from NSObject, Swift is trying to generate getters and setters for it. Because you are creating it in swift, using the @nonobjc attribute solves the problem.

EDIT:

I don't believe you can observe static variables through KVO because of how it works

Here is a link and snippet from Apple's Guide on KVO

Unlike notifications that use NSNotificationCenter, there is no central object that provides change notification for all observers. Instead, notifications are sent directly to the observing objects when changes are made.

Perhaps, instead of using KVO, you could declare online like:

static var online = false {
    didSet{
        //code to post notification through regular notification center
    }
}

If you're set on using it, this question might point you towards the right direction — it'll involve diving deeper into how KVO works: Is it possible to set up KVO notifications for static variables in objective C?