Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no reason to subclass <code>NSNotification</code> the way you're describing. First, <code>NSNotification</code> already carries a userInfo dictionary. You can put any data you want in there. You can use category methods to read and write into that dictionary if you like (I do this all the time). For example, a very common thing I want to do is pass along some object, say the <code>RNMessage</code>. So I create a category that looks like this:</p> <pre><code>@interface NSNotificationCenter (RNMessage) - (void)postNotificationName:(NSString *)aName object:(id)anObject message:(RNMessage *)message; @end @interface NSNotification (RNMessage) - (RNMessage *)message; @end </code></pre> <hr> <pre><code>static NSString * const RNMessageKey = @"message"; @implementation NSNotificationCenter (RNMessage) - (void)postNotificationName:(NSString *)aName object:(id)anObject message:(RNMessage *)message { [self postNotificationName:aName object:anObject userInfo:[NSDictionary dictionaryWithObject:message forKey:RNMessageKey]; } @end @implementation NSNotification (RNMessage) - (RNMessage *)message { return [[self userInfo] objectForKey:RNMessageKey]; } </code></pre> <p>As @hypercrypt notes, you can also use associated references to attach data to any arbitrary object without creating an ivar, but with <code>NSNotification</code> it's much simpler to use the userInfo dictionary. It's much easier to print notification using <code>NSLog</code>. Easier to serialize them. Easier to copy them. Etc. Associated references are great, but they do add lots of little corner cases that you should avoid if you can get away with it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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