带有 NULL 对象的 NSNotificationCenter.PostNotificationName() 不会触发:错误还是按设计?

发布于 2024-12-07 04:13:34 字数 484 浏览 0 评论 0原文

我使用如下代码订阅我自己的通知:

NSNotificationCenter.DefaultCenter.AddObserver("BL_UIIdleTimerFired", delegate {
    Console.WriteLine("BaseFolderViewController: idle timer fired");
});

发送通知:

NSNotificationCenter.DefaultCenter.PostNotificationName("BL_UIIdleTimerFired", null);

但是,只有当 PostNotificationName(string sString, object anObject) 的“anObject”参数为不为空。

这是设计使然吗?我必须传递一个对象吗?或者这是一个错误? 我真的不想发送对特定对象的引用。

I'm subscribing to my own notifications using code like this:

NSNotificationCenter.DefaultCenter.AddObserver("BL_UIIdleTimerFired", delegate {
    Console.WriteLine("BaseFolderViewController: idle timer fired");
});

To send a notification:

NSNotificationCenter.DefaultCenter.PostNotificationName("BL_UIIdleTimerFired", null);

However, the notification will only be received correctly if the "anObject" parameter of PostNotificationName(string sString, object anObject) is not NULL.

Is this by design? Do I have to pass an object? Or is it a bug?
I don't really want to send a reference to a specific object.

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

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

发布评论

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

评论(2

夏雨凉 2024-12-14 04:13:34

这是 MonoTouch 中的一个错误。 NSNotification 的构建是为了让您可以发送一个可选的字典和一个可选的对象,该对象通常是发送者,但也可以是其他对象。这两个都可以为 null,但在 MonoTouch 中传递 null 作为对象参数会导致空指针异常。

关于 Object 参数,iOS 文档非常清楚:
与通知关联的对象。这通常是发布此通知的对象。它可能为零。

public void SendNotification()
{
    NSNotification notification = NSNotification.FromName("AwesomeNotification",new NSObject());            
    NSNotificationCenter.DefaultCenter.PostNotification(notification);
}

public void StartListeningForNotification()
{
    NSString name = new NSString("AwesomeNotification");
    NSNotificationCenter.DefaultCenter.AddObserver(this,new Selector("AwesomeNotificationReceived:"),name,null);            
}

public void StopListeningForNotification()
{
    NSNotificationCenter.DefaultCenter.RemoveObserver(this,"AwesomeNotification",null);             
}

[Export("AwesomeNotificationReceived:")]
public void AwesomeNotificationReceived(NSNotification n)
{

}

This is a bug in MonoTouch. NSNotification is built so you can send an optional dictionary and an optional object, which is normally the sender, but can be other objects as well. Both of these can be null, but in MonoTouch passing a null as the object parameter causes a Null Pointer Exception.

Its quite clear from the iOS documentation, regarding the Object parameter:
The object associated with the notification. This is often the object that posted this notification. It may be nil.

public void SendNotification()
{
    NSNotification notification = NSNotification.FromName("AwesomeNotification",new NSObject());            
    NSNotificationCenter.DefaultCenter.PostNotification(notification);
}

public void StartListeningForNotification()
{
    NSString name = new NSString("AwesomeNotification");
    NSNotificationCenter.DefaultCenter.AddObserver(this,new Selector("AwesomeNotificationReceived:"),name,null);            
}

public void StopListeningForNotification()
{
    NSNotificationCenter.DefaultCenter.RemoveObserver(this,"AwesomeNotification",null);             
}

[Export("AwesomeNotificationReceived:")]
public void AwesomeNotificationReceived(NSNotification n)
{

}
白日梦 2024-12-14 04:13:34

我认为这是设计使然。苹果关于其他重载的文档(postNotificationName:object:userInfo:) 表示 userInfo 参数可以为 null。所以我想其他两个不能为空。

“anObject”参数是发布通知的对象(发送者),也是可以从 NSNotification 类的 Object 参数中检索的对象。

I think this is by design. Apple's documentation for the other overload (postNotificationName:object:userInfo:) states that the userInfo parameter can be null. So I suppose the other two cannot be null.

The "anObject" parameter is the object that posts the notification (sender) and the one that can be retrieved from NSNotification class' Object parameter.

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