Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>The first NSLog works fine displaying the contents of the dictionary. The second log throws the following exception...</p> </blockquote> <p>The program tries to trick you, it just looks like it is your dictionary because your dictionary is inside the notification. From the exception you can see that your object actually is from a class named NSConcreteNotification.<br> This is because the argument of a notification-method is always a NSNotification-object. this will work: </p> <pre><code>- (void)hotSpotMore:(NSNotification *)notification { NSLog(@"%@", notification.object); NSLog(@"%@", [notification.object objectForKey:@"HelpTopic"]); } </code></pre> <hr> <p>just as a hint: the object is usually the object which sends the notification, you should send your NSDictionary as userInfo.<br> I think it would improve your code if you would do it like this:</p> <pre><code>NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center postNotificationName:@"HotSpotTouched" object:self userInfo:itemDetails]; - (void)hotSpotMore:(NSNotification *)notification { NSLog(@"%@", notification.userInfo); NSLog(@"%@", [notification.userInfo objectForKey:@"HelpTopic"]); } </code></pre> <p>The object parameter is used to distinguish between the different objects that can send a notification.<br> Let’s say you have two different HotSpot objects that can both send the notification. When you set the <code>object</code> in <code>addObserver:selector:name:object:</code> you can add a different observer for each of the objects. Using nil as the object parameter means that all notifications should be received, regardless of the object that did send the notification.</p> <p>E.g:</p> <pre><code>FancyHotSpot *hotSpotA; FancyHotSpot *hotSpotB; // notifications from hotSpotA should call hotSpotATouched: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hotSpotATouched:) name:@"HotSpotTouched" object:hotSpotA]; // only notifications from hotSpotA will be received // notifications from hotSpotB should call hotSpotBTouched: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hotSpotBTouched:) name:@"HotSpotTouched" object:hotSpotB]; // only notifications from hotSpotB will be received // notifications from all objects should call anyHotSpotTouched: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(anyHotSpotTouched:) name:@"HotSpotTouched" object:nil]; // nil == “any object”, so all notifications with the name “HotSpotTouched” will be received - (void)hotSpotATouched:(NSNotification *)n { // only gets notification of hot spot A } - (void)hotSpotBTouched:(NSNotification *)n { // only gets notification of hot spot B } - (void)anyHotSpotTouched:(NSNotification *)n { // catches all notifications } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload