如何在Objective-C中创建NSNotification的对象?

Cat*_*thy 5 objective-c

我想创建一个NSNotification的对象,如下所示:

NSNotification *obj=[[NSNotification alloc]init];
Run Code Online (Sandbox Code Playgroud)

但是当我这样创建时,我得到一个例外,因为'NSConcreteNotification init:is not allowed'.我该如何解决这个问题?

out*_*tis 7

NSNotification文档:

您可以使用类方法notificationWithName:object:或创建通知对象notificationWithName:object:userInfo:.但是,您通常不会直接创建自己的通知.该NSNotificationCenter方法postNotificationName:object:,并postNotificationName:object:userInfo:允许您方便地发布通知,而不首先创建它.