自定义NSUserNotifictation对象

时间:2016-12-21 14:12:12

标签: objective-c macos cocoa nsusernotification

我正在尝试修改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对象会涉及?

1 个答案:

答案 0 :(得分:2)

所以这堂课似乎并没有被覆盖,但好消息是:有一个userInfo字典?

所以你可以在userInfo dict中存储任何对象作为KV对...

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