我想创建一个NSNotification的子类。我不想创建一个类别或其他任何东西.
您可能知道,NSNotification是一个类集群,如NSArray或NSString。
我知道集群类的子类需要:
这是我的子类(没什么稀奇的):
@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当我使用它时,我会得到一个非凡的:
*** 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!'为了从NSNotification继承,我还需要做什么?
发布于 2015-02-06 19:09:34
问题是试图调用超类初始化器。你不能这样做,因为这是一个抽象类。所以,在初始化器中,您只需插入您的存储。
因为这太可怕了,我最终为NSNotification创建了一个类别。在这里,我添加了三种方法:
userInfo配置为用作存储。userInfo。最后,这个类别只是一个处理userInfo的助手。
谢谢@Paulw11的评论!
https://stackoverflow.com/questions/28356714
复制相似问题