How to create an NSNotification object in Objective-C?

I want to create an NSNotification object, for example:

NSNotification *obj=[[NSNotification alloc]init];

but when I create this, I get an exception like "NSConcreteNotification init: not allowed". How to solve this problem?

+5
source share
2 answers

From the NSNotification document :

You can create a notification object using the class methods notificationWithName:object:or notificationWithName:object:userInfo:. However, you usually do not create your own notifications directly. NSNotificationCenter methods postNotificationName:object:and postNotificationName:object:userInfo:allow you to conveniently place a notification without creating it in the first place.

+7
source

NSNotificationCenter has convenient methods for creating and sending notifications:

[[NSNotificationCenter defaultCenter] 
               postNotificationName:XYYourNotification
               object:@"someObject"];

, extern:

extern NSString* const XYYourNotification;

NSString * .
, .

+2

All Articles