我想创建一个NSNotification对象,代码如下:
NSNotification *obj=[[NSNotification alloc]init];
但是当我像这样创建时,我会收到一个异常,提示“NSConcreteNotification init: is not allowed”。我该如何解决这个问题?
我想创建一个NSNotification对象,代码如下:
NSNotification *obj=[[NSNotification alloc]init];
notificationWithName:object:
或notificationWithName:object:userInfo:
创建通知对象。但是,通常情况下不直接创建自己的通知。NSNotificationCenter方法postNotificationName:object:
和postNotificationName:object:userInfo:
允许您方便地发布通知而无需先创建它。NSNotificationCenter有方便的方法来构建和发送通知:
[[NSNotificationCenter defaultCenter]
postNotificationName:XYYourNotification
object:@"someObject"];
extern NSString* const XYYourNotification;
在你的实现中定义实际的NSString*。
如果你使用字符串常量作为通知名称,你的代码就不容易出现打字错误。
FOUNDATION_EXPORT
而不是extern
。在Objective-C文件中,它将是extern
,在Objective-C++项目中,它将是extern "C"
,如果你发现自己在使用它们。 - Brandon Buck