access object passed in NSNotification?

21,270

Solution 1

it's [notification object]

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

Solution 2

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]);
}

Solution 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.

Solution 4

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.

Solution 5

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)
}
Share:
21,270

Related videos on Youtube

Slee
Author by

Slee

Updated on November 02, 2021

Comments

  • Slee
    Slee over 2 years

    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.

  • ColdLogic
    ColdLogic almost 13 years
    Need to register for the notification. Just a little something you left out, :P
  • ColdLogic
    ColdLogic almost 13 years
    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.
  • PeyloW
    PeyloW almost 13 years
    @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 :).