我想創建NSNotification的對象說:如何在Objective-C中創建NSNotification對象?
NSNotification *obj=[[NSNotification alloc]init];
但是當我創建這樣我得到一個例外,因爲「NSConcreteNotification INIT:不允許」。 我應該如何解決這個問題?
我想創建NSNotification的對象說:如何在Objective-C中創建NSNotification對象?
NSNotification *obj=[[NSNotification alloc]init];
但是當我創建這樣我得到一個例外,因爲「NSConcreteNotification INIT:不允許」。 我應該如何解決這個問題?
從NSNotification文檔:
您可以創建一個類的方法
notificationWithName:object:
或notificationWithName:object:userInfo:
通知對象。但是,您通常不會直接創建自己的通知。 NSNotificationCenter方法postNotificationName:object:
和postNotificationName:object:userInfo:
使您可以方便地發佈通知,而無需先創建通知。
NSNotificationCenter具有方便的方法來構建和調度通知:
[[NSNotificationCenter defaultCenter]
postNotificationName:XYYourNotification
object:@"someObject"];
如果你想使用自己的通知,創建通知名稱EXTERN:
extern NSString* const XYYourNotification;
,並確定實際的NSString *在您的實施中。
如果您使用字符串常量作爲通知名稱,那麼您的代碼不太容易出錯。
我意識到這是一個古老的答案,但不是'extern'你應該使用'FOUNDATION_EXPORT'這將是'extern'在Objective-C文件或'的extern「C」'在Objective-C++項目應你發現自己使用它們。 – 2013-06-25 16:15:36