为 NSNotification 注册 bool

发布于 2024-09-09 09:04:16 字数 680 浏览 2 评论 0原文

我试图将我的注意力集中在 NSNotification 上,但似乎无法让它工作。我认为我误解了如何注册通知。

我的连接管理器类中有一个 bool 作为属性。初始化时,我使用一些服务器进行身份验证,并检查是否可以访问外部 URL(应用程序主要用于公司内部网,外部连接并不总是可行)

如果无法访问,BOOL 属性将从 YES 更改为 NO连接,并且由于可以随时响应,因此我认为最好在其更改时注册通知。该属性称为 externalConnectionAvailable

[ConnectionManager addObserver:self forKeyPath:@"externalConnectionAvailable" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];

并具有方法:

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"observer called");
}

但这不会被调用。我做错了什么吗?

谢谢

I'm trying to wrap my head around NSNotification but can't seem to get it to work. Think I'm misunderstanding how to register for an notification.

I have a bool as a property in my connection manager class. At initialisation I authenticate with a few servers and check if I can access an external URL (App will mainly be used on company intranet and an external connection isn't always possible)

The BOOL property will be changed from YES to NO if it cannot access the connection and as this can be responded at any time I thought it would be best to register a notification for when it changes. The property is called externalConnectionAvailable

[ConnectionManager addObserver:self forKeyPath:@"externalConnectionAvailable" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];

and have the method:

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"observer called");
}

But this doesn't get called. Am I doing something completely wrong?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

開玄 2024-09-16 09:04:16

在这个声明中:

[ConnectionManager addObserver:self forKeyPath:@"externalConnectionAvailable" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];

假设您遵循“Cocoa Way”并使用类和对象实例的常用命名方案,您似乎正在尝试为整个类(而不是对象实例)添加观察者。

你应该有类似的东西

ConnectionManager *connectionManagerInstance = // initialize manager...
...
[connectionManagerInstance addObserver:self forKeyPath:@"externalConnectionAvailable" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];

In this statement:

[ConnectionManager addObserver:self forKeyPath:@"externalConnectionAvailable" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];

Assuming that you are following the "Cocoa Way" and using the usual naming scheme for classes and object instances, you appear to be trying to add an observer for an entire class, as opposed to an object instance.

You should have something like

ConnectionManager *connectionManagerInstance = // initialize manager...
...
[connectionManagerInstance addObserver:self forKeyPath:@"externalConnectionAvailable" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];
霓裳挽歌倾城醉 2024-09-16 09:04:16

这是一件非常愚蠢的事情。我只是通过调用 externalConnectionAvailable 而不是 self.externalConnectionAvailable 来更改属性

It was something very stupid. I was just changing the property by calling externalConnectionAvailable not self.externalConnectionAvailable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文