Ios 如何在头文件中记录NSNotificationCenter通知

Ios 如何在头文件中记录NSNotificationCenter通知,ios,header-files,nsnotificationcenter,foundation,Ios,Header Files,Nsnotificationcenter,Foundation,我正在使用NSNotificationCenter类进行广播,当我的应用程序的模式发生变化时,其他类可能会感兴趣。我遵循的是标准做法,类似这样: NSNumber *myData = [NSNumber numberWithInt:42]; NSDictionary *myDict = [NSDictionary dictionaryWithObject:myData forKey:@"dat

我正在使用
NSNotificationCenter
类进行广播,当我的应用程序的模式发生变化时,其他类可能会感兴趣。我遵循的是标准做法,类似这样:

NSNumber *myData = [NSNumber numberWithInt:42];
NSDictionary *myDict =
            [NSDictionary dictionaryWithObject:myData
                                        forKey:@"data"];

NSString *myNotificationKey = @"mynote";
[[NSNotificationCenter defaultCenter] postNotificationName:myNotificationKey
                                                    object:self
                                                  userInfo:myDict];
没有什么新鲜事。不过,我的问题是:我应该如何“声明”我可能发布的通知,以便其他开发人员知道应该听什么?我的意思不是字面上的声明,但除了编写单独的文档外,我应该如何传达期望的内容?我希望使用我的类的人能够查看头文件并确定他们可以期望的通知。我可以这样做

// in MyClass.h

/*
 * NOTIFICATIONS
 * Name: mynote
 * UserInfo: {data : (NSNumber *)}
 * Name: myothernote
 * etc....
 */
但那很笨重。是将此类信息放在单独文档中的唯一选项吗?

以下是我使用的:

在头文件中,在
#导入
行之后,但在
@接口
声明之前:

   // Documentation about why the notification is interesting and useful
   extern NSString * const ZZZSomeNameNotification;
在实现文件中:

   NSString * const ZZZSomeNotification = @"ZZZSomeNotification";
然后,当在代码中发布通知时,在引用名称时使用ZZZSomeNotification


这与苹果在其头文件中声明通知名的方式非常相似。通过文件菜单中Xcode的“快速打开…”菜单项查看您使用的Apple提供的类的头文件,您将看到类似的内容。

侧注-使用新的Objective-C文字语法:
NSDictionary*myDict={@“data”:@42}。谢谢!只有一个旁注:我相信你遗漏了一个前导的
@
;应该是
NSDictionary*myDict=@{@“data”:@42}。是的,我做了。很抱歉好主意。我真的很喜欢这个解决方案。我不是很高兴这是最终的解决方案,但它似乎是我将得到的最好的解决方案。