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.

IPS Brar
  • 346
  • 1
  • 14
Slee
  • 27,498
  • 52
  • 145
  • 243

8 Answers8

34

it's [notification object]

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

alex-i
  • 5,406
  • 2
  • 36
  • 56
  • 9
    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. – ColdLogic Jul 19 '11 at 14:35
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]);
}
ColdLogic
  • 7,206
  • 1
  • 28
  • 46
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.

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
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)
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Juan Boero
  • 6,281
  • 1
  • 44
  • 62
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.

Joris Mans
  • 6,024
  • 6
  • 42
  • 69
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]);
}
PeyloW
  • 36,742
  • 12
  • 80
  • 99
  • Need to register for the notification. Just a little something you left out, :P – ColdLogic Jul 19 '11 at 14:33
  • @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 :). – PeyloW Jul 19 '11 at 14:40
0

More simpler way is

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

It worked for me.

Soorej Babu
  • 350
  • 2
  • 19
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]];
crifan
  • 12,947
  • 1
  • 71
  • 56