2016-12-21 34 views
1

我想修改NSUserNotifictation通過自定義變量:定製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對象涉入?

回答

2

所以這個類並不意味着被覆蓋似乎,但是好消息:有一個userInfo字典?

所以,你可以存儲我會使用任何對象作爲一個用戶信息字典KV對...

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

乾杯:https://developer.apple.com/reference/foundation/nsusernotification/1415675-userinfo對自定義對象感到興奮 – maxisme

+1

@Maximilian它以這種方式工作的原因永遠不會對我們凡人顯而易見,但它是通過類集羣實現的,通常我們永遠不需要知道「爲什麼」。 –