我正在尝试修改NSUserNotifictation以传递自定义变量:
@interface MyUserNotification: NSUserNotification
@property (nonatomic) int theid;
@property (nonatomic) NSString* url;
@end
@implementation MyUserNotification
@end
然后在我的
AppDelegate
对象中初始化时:MyUserNotification *notification = [[MyUserNotification alloc] init];
notification.theid = theid;
设置
theid
会引发错误:-[_NSConcreteUserNotification setTheid:]: unrecognized selector sent to instance 0x100204840
为什么
NSConcreteUserNotification
对象参与其中? 最佳答案
因此,此类似乎不是要重写的,而是个好消息:是否有userInfo格?
因此,您可以将任何对象作为KV对存储在userInfo字典中。
int theid;
NSUserNotification *notification = [[NSUserNotification alloc] init];
NSDictionary * userInfo = [NSMutableDictionary dictionary];
[userInfo setValue:@(theid) forKey:@"theid"];
notification.userInfo = userInfo;