access object passed in NSNotification?
Asked Answered
R

8

23

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.

Rectangle answered 19/7, 2011 at 14:16 Comment(1)
Swift versionSeligmann
B
34

it's [notification object]

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

Brachy answered 19/7, 2011 at 14:22 Comment(1)
Object is not meant to be a way to store data passed with the notification. It's meant to be the sender of the notification, that way you can figure out what type of object sent the notification and act accordingly. Using notificationWithName:object:userInfo: is the way to go here.Heavyarmed
H
15

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]);
}
Heavyarmed answered 19/7, 2011 at 14:25 Comment(0)
G
3

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.

Grapnel answered 3/6, 2016 at 10:6 Comment(0)
A
2

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.

Ayotte answered 19/7, 2011 at 14:23 Comment(0)
S
2

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)
}
Seligmann answered 11/2, 2017 at 20:1 Comment(0)
S
1

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]);
}
Ship answered 19/7, 2011 at 14:30 Comment(2)
Need to register for the notification. Just a little something you left out, :PHeavyarmed
@ColdLogic: True. I intentionally left that out for brevity, since the original question already contained that information,I am confident that Slee already understand that :).Ship
S
0

More simpler way is

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

It worked for me.

Stodgy answered 20/3, 2019 at 11:55 Comment(0)
F
0

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]];
Frye answered 2/11, 2021 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.