addObserver:forKeyPath: 如何在静态类上工作?
我正在尝试构建一个名为 Logger 的静态类,当像 [Logger uploadLogFiles]
那样调用时,它将在某个时刻上传日志文件。
我试图向这个静态类添加一个观察者,如下所示:
[Logger addObserver:self forKeyPath:@"uploadComplete" options:NSKeyValueObservingOptionNew context:nil];
我在开始异步调用之前执行此操作NSURLConnection 的方法。我确实收到一条警告,指出不兼容的指针类型将 Class
发送到 NSObject *
类型的参数。
但是,这似乎不起作用,因为 observerValueForKeyPath: 方法永远不会被调用。
有人有在静态类中向静态变量添加观察者的经验吗?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类没有实例变量,因此没有什么可观察的。
您需要有一个用于 KVO 的实际对象(类的实例)。
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.