I want to create an object of NSNotification as say:
NSNotification *obj=[[NSNotification alloc]init];
but when i create like this i get an exception as 'NSConcreteNotification init: is not allowed'. How should i solve this problem?
An object containing information broadcast to registered observers that bridges to Notification ; use NSNotification when you need reference semantics or other Foundation-specific behavior.
A structure that defines the name of a notification.
A notification dispatch mechanism that enables the broadcast of information to registered observers.
How To: Using Notification Center In Swift. With NotificationCenter you can broadcast data from one part of your app to another. It uses the Observer pattern to inform registered observers when a notification comes in, using a central dispatcher called Notification Center.
From the NSNotification documentation:
You can create a notification object with the class methods
notificationWithName:object:
ornotificationWithName:object:userInfo:
. However, you don’t usually create your own notifications directly. The NSNotificationCenter methodspostNotificationName:object:
andpostNotificationName:object:userInfo:
allow you to conveniently post a notification without creating it first.
NSNotificationCenter has convenience methods to construct and dispatch notifications:
[[NSNotificationCenter defaultCenter]
postNotificationName:XYYourNotification
object:@"someObject"];
If you want to use your own notifications, create the notification name extern:
extern NSString* const XYYourNotification;
and define the actual NSString* in your implementation.
If you use string constants for your notification names, your code is less error-prone to typos.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With