自定义 NSUserNotification 对象

Custom NSUserNotifictation object

我正在尝试修改 NSUserNotification 以传递自定义变量:

@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 对象参与其中?

所以这个 class 看起来并不意味着被覆盖,但好消息:有一个 userInfo 字典?

因此您可以将任何对象作为 KV 对存储在 userInfo 字典中...

int theid;
NSUserNotification *notification = [[NSUserNotification alloc] init];
NSDictionary * userInfo = [NSMutableDictionary dictionary];
[userInfo setValue:@(theid) forKey:@"theid"];
notification.userInfo = userInfo;