Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Subclassing <code>NSNotification</code> is an atypical operation. I think I've only seen it done once or twice in the past few years.</p> <p>If you're looking to pass things along with the notification, that's what the <code>userInfo</code> property is for. If you don't like accessing things through the <code>userInfo</code> directly, you could use a category to simplify access:</p> <pre><code>@interface NSNotification (EasyAccess) @property (nonatomic, readonly) NSString *foo; @property (nonatomic, readonly) NSNumber *bar; @end @implementation NSNotification (EasyAccess) - (NSString *)foo { return [[self userInfo] objectForKey:@"foo"]; } - (NSNumber *)bar { return [[self userInfo] objectForKey:@"bar"]; } @end </code></pre> <p>You can also use this approach to simplify <code>NSNotification</code> creation. For example, your category could also include:</p> <pre><code>+ (id)myNotificationWithFoo:(NSString *)foo bar:(NSString *)bar object:(id)object { NSDictionary *d = [NSDictionary dictionaryWithObjectsForKeys:foo, @"foo", bar, @"bar", nil]; return [self notificationWithName:@"MyNotification" object:object userInfo:d]; } </code></pre> <p>If, for some strange reason, you'd need the properties to be mutable, then you'd need to use <a href="http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocAssociativeReferences.html" rel="noreferrer">associative references</a> to accomplish that:</p> <pre><code>#import &lt;objc/runtime.h&gt; static const char FooKey; static const char BarKey; ... - (NSString *)foo { return (NSString *)objc_getAssociatedObject(self, &amp;FooKey); } - (void)setFoo:(NSString *)foo { objc_setAssociatedObject(self, &amp;FooKey, foo, OBJC_ASSOCIATION_RETAIN); } - (NSNumber *)bar { return (NSNumber *)objc_getAssociatedObject(self, &amp;BarKey); } - (void)setBar:(NSNumber *)bar { objc_setAssociatedObject(self, &amp;BarKey, bar, OBJC_ASSOCIATION_RETAIN); } ... </code></pre>
    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