访问 NSNotification 中传递的对象?

发布于 2024-11-25 01:46:37 字数 1115 浏览 4 评论 0原文

我有一个正在发布 NSDictionary 的 NSNotification:

 NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                          anItemID, @"ItemID",
                                          [NSString stringWithFormat:@"%i",q], @"Quantity",
                                          [NSString stringWithFormat:@"%@",[NSDate date]], @"BackOrderDate",
                                          [NSString stringWithFormat:@"%@", [NSDate date]],@"ModifiedOn",
                                          nil];

                    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"InventoryUpdate" object:dict]];

如何订阅此内容并从此 NSDictionary 获取信息?

在我的 viewDidLoad 中,我有:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveInventoryUpdate:) name:@"InventoryUpdate" object:nil];

和类中的一个方法:

- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"%@ updated", [notification userInfo]);
}

当然,它会记录一个空值。

I have a NSNotification that is posting a NSDictionary:

 NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                          anItemID, @"ItemID",
                                          [NSString stringWithFormat:@"%i",q], @"Quantity",
                                          [NSString stringWithFormat:@"%@",[NSDate date]], @"BackOrderDate",
                                          [NSString stringWithFormat:@"%@", [NSDate date]],@"ModifiedOn",
                                          nil];

                    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"InventoryUpdate" object:dict]];

How do I subscribe to this and get information from this NSDictionary?

in my viewDidLoad I have:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveInventoryUpdate:) name:@"InventoryUpdate" object:nil];

and a method in the class:

- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"%@ updated", [notification userInfo]);
}

which logs a null value of course.

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

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

发布评论

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

评论(8

岁月静好 2024-12-02 01:46:38

你做错了。您需要使用:

-(id)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)userInfo

并将字典传递给最后一个参数。您的“object”参数是发送通知的对象,而不是字典。

You are doing it wrong. You need to use:

-(id)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)userInfo

and pass the dict to the last parameter. Your "object" parameter is the object sending the notification and not the dictionary.

已下线请稍等 2024-12-02 01:46:38

斯威夫特:

// Propagate notification:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: ["info":"your dictionary"])

// Subscribe to notification:
NotificationCenter.default.addObserver(self, selector: #selector(yourSelector(notification:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)

// Your selector:
func yourSelector(notification: NSNotification) {
    if let info = notification.userInfo, let infoDescription = info["info"] as? String {
            print(infoDescription)
        } 
}

// Memory cleaning, add this to the subscribed observer class:
deinit {
    NotificationCenter.default.removeObserver(self)
}

Swift:

// Propagate notification:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: ["info":"your dictionary"])

// Subscribe to notification:
NotificationCenter.default.addObserver(self, selector: #selector(yourSelector(notification:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)

// Your selector:
func yourSelector(notification: NSNotification) {
    if let info = notification.userInfo, let infoDescription = info["info"] as? String {
            print(infoDescription)
        } 
}

// Memory cleaning, add this to the subscribed observer class:
deinit {
    NotificationCenter.default.removeObserver(self)
}
南街九尾狐 2024-12-02 01:46:38

通知中的对象旨在作为发送者,在您的情况下,字典实际上并不是发送者,它只是信息。与通知一起发送的任何辅助信息都旨在与 userInfo 字典一起传递。像这样发送通知:

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                      anItemID, 
                                      @"ItemID",
                                      [NSString stringWithFormat:@"%i",q], 
                                      @"Quantity",
                                      [NSString stringWithFormat:@"%@", [NSDate date]], 
                                      @"BackOrderDate",
                                      [NSString stringWithFormat:@"%@", [NSDate date]],
                                      @"ModifiedOn",
                                      nil];

[[NSNotificationCenter defaultCenter] postNotification:
        [NSNotification notificationWithName:@"InventoryUpdate" 
                                      object:self 
                                    userInfo:dict]];

然后像这样接收它,以良好的方式获得您想要的行为:

- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"%@ updated", [notification userInfo]);
}

The object from the notification is intended to be the sender, in your case the dictionary is not actually the sender, its just information. Any auxiliary information to be sent along with the notification is intended to be passed along with the userInfo dictionary. Send the notification as such:

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                      anItemID, 
                                      @"ItemID",
                                      [NSString stringWithFormat:@"%i",q], 
                                      @"Quantity",
                                      [NSString stringWithFormat:@"%@", [NSDate date]], 
                                      @"BackOrderDate",
                                      [NSString stringWithFormat:@"%@", [NSDate date]],
                                      @"ModifiedOn",
                                      nil];

[[NSNotificationCenter defaultCenter] postNotification:
        [NSNotification notificationWithName:@"InventoryUpdate" 
                                      object:self 
                                    userInfo:dict]];

And then receive it like this, to get the behavior you intend in a good way:

- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"%@ updated", [notification userInfo]);
}
撞了怀 2024-12-02 01:46:38

更简单的方法是

-(void)recieveInventoryUpdate:(NSNotification *)notification
{
    NSLog(@"%@ updated",[notification object]);
    //Or use notification.object
}

它对我有用。

More simpler way is

-(void)recieveInventoryUpdate:(NSNotification *)notification
{
    NSLog(@"%@ updated",[notification object]);
    //Or use notification.object
}

It worked for me.

荆棘i 2024-12-02 01:46:38

简单答案

详细答案

完整代码应该是:

在您的viewDidLoad中定义

  • 观察者 code>
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveInventoryUpdate:) name:@"InventoryUpdate" object:nil];
  • 并处理通知方法
- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"notification=%@", notification);
    // NSDictionary* yourPassedInDict = notification.userInfo;
    NSDictionary* yourPassedInDict = [notification userInfo];
    NSLog(@"dict=%@", yourPassedInDict);
}

用法 = 调用者

  • 发布 NSDictionary
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
    anItemID, @"ItemID",
    [NSString stringWithFormat:@"%i",q], @"Quantity",
    [NSString stringWithFormat:@"%@",[NSDate date]], @"BackOrderDate",
    [NSString stringWithFormat:@"%@", [NSDate date]],@"ModifiedOn",
    nil];

[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"InventoryUpdate" object:self userInfo:dict]];

Simple Answer

Detail Answer

full code should be:

definition

  • observer in your viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveInventoryUpdate:) name:@"InventoryUpdate" object:nil];
  • and handle notification method
- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"notification=%@", notification);
    // NSDictionary* yourPassedInDict = notification.userInfo;
    NSDictionary* yourPassedInDict = [notification userInfo];
    NSLog(@"dict=%@", yourPassedInDict);
}

usage = caller

  • posting a NSDictionary
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
    anItemID, @"ItemID",
    [NSString stringWithFormat:@"%i",q], @"Quantity",
    [NSString stringWithFormat:@"%@",[NSDate date]], @"BackOrderDate",
    [NSString stringWithFormat:@"%@", [NSDate date]],@"ModifiedOn",
    nil];

[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"InventoryUpdate" object:self userInfo:dict]];
绳情 2024-12-02 01:46:37

它是[notification object]

您还可以使用notificationWithName:object:userInfo:方法发送用户信息

it's [notification object]

you can also send userinfo by using notificationWithName:object:userInfo: method

林空鹿饮溪 2024-12-02 01:46:37

对象是发布通知的对象,而不是存储对象以便您可以访问它的方式。用户信息是您存储要与通知一起保留的信息的位置。

[[NSNotificationCenter defaultCenter] postNotificationName:@"Inventory Update" object:self userInfo:dict];

然后注册通知。该对象可以是您的类,也可以是 nil 以接收该名称的所有通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveInventoryUpdate:) name:@"InventoryUpdate" object:nil];

接下来在您的选择器中使用它

- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"%@ updated", [notification userInfo]);
}

Object is what object is posting the notification, not a way to store the object so you can get to it. The user info is where you store information you want to keep with the notification.

[[NSNotificationCenter defaultCenter] postNotificationName:@"Inventory Update" object:self userInfo:dict];

Then register for the notification. The object can be your class, or nil to just receive all notifications of this name

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveInventoryUpdate:) name:@"InventoryUpdate" object:nil];

Next use it in your selector

- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"%@ updated", [notification userInfo]);
}
乄_柒ぐ汐 2024-12-02 01:46:37

很简单,看下面

- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"%@ updated",notification.object); // gives your dictionary 
    NSLog(@"%@ updated",notification.name); // gives keyname of notification

}

如果访问notification.userinfo,它会返回null

It's simple, see below

- (void)recieveInventoryUpdate:(NSNotification *)notification {
    NSLog(@"%@ updated",notification.object); // gives your dictionary 
    NSLog(@"%@ updated",notification.name); // gives keyname of notification

}

if access the notification.userinfo, it will return null.

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