I want to create a subclass of NSNotification
. I don't want to create a category or anything else.
As you may know NSNotification
is a Class Cluster, like NSArray
or NSString
.
I'm aware that a subclass of a cluster class needs to:
This is my subclass (nothing fancy):
@interface MYNotification : NSNotification
@end
@implementation MYNotification
- (NSString *)name { return nil; }
- (id)object { return nil; }
- (NSDictionary *)userInfo { return nil; }
- (instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo
{
return self = [super initWithName:name object:object userInfo:userInfo];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
return self = [super initWithCoder:aDecoder];
}
@end
When I use it, I get an extraordinary:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** initialization method -initWithName:object:userInfo: cannot be sent to an abstract object of class MYNotification: Create a concrete instance!'
What else do I have to do in order to inherit from NSNotification
?
The problem was that was trying to call the superclass initialiser. You can't do that because is an abstract class. So, in the initializer you just have to init your storage.
Because this is horrible, I end up creating a category for NSNotification
instead. There I added three kind methods:
userInfo
to be used as the storage.userInfo
.In the end, the category it's just a helper to deal with userInfo
.
Thanks @Paulw11 for your comment!