I am trying to subclass NSNotification.
Apple's docs for NSNotificationstate the following:
NSNotificationis a class cluster with no instance variables. As such, you must subclassNSNotificationand override the primitive methodsname,object, anduserInfo. You can choose any designated initializer you like, but be sure that your initializer does not callNSNotification’s implementation ofinit(via[super init]).NSNotificationis not meant to be instantiated directly, and itsinitmethod raises an exception.
But this isn't clear to me. Should I create an initializer like this?
-(id)initWithObject:(id)object
{
return self;
}
Subclassing
NSNotificationis an atypical operation. I think I've only seen it done once or twice in the past few years.If you're looking to pass things along with the notification, that's what the
userInfoproperty is for. If you don't like accessing things through theuserInfodirectly, you could use a category to simplify access:You can also use this approach to simplify
NSNotificationcreation. For example, your category could also include:If, for some strange reason, you'd need the properties to be mutable, then you'd need to use associative references to accomplish that: