How does addObserver:forKeyPath: work on a static

2019-09-04 18:41发布

问题:

I'm trying to build a static class called Logger that will upload the log files at some point, when called like [Logger uploadLogFiles].

I'm trying to add an observer to this static class like so:

[Logger addObserver:self forKeyPath:@"uploadComplete" options:NSKeyValueObservingOptionNew context:nil];

I do this just before starting an asynchronous call method for NSURLConnection. I do get a warning, saying Incompatible pointer types sending Class to parameter of type NSObject *.

However, this does not seem to work, as the observerValueForKeyPath: method never gets called.

Has anybody had any experience with adding observers to static variables in static classes?

Thanks!

回答1:

A Class has no instance variables, so there is nothing to observe.

You need to have an actual object (an instance of a class) for KVO.