NSNotifications 命名最佳实践

发布于 2024-11-02 17:54:32 字数 626 浏览 0 评论 0原文

在尝试将我的模型与显示获取的数据的视图控制器分离时,当异步获取完成时,我发布一个 NSNotification。

 [[NSNotificationCenter defaultCenter] postNotificationName:@"foobarFetchSuccess" object: foo];

我已经养成了在公共头文件中使用:的习惯

 #define FOO_FETCH_SUCCESS  @"foobarFetchSuccess"

,然后将其用于 addObserver: 和 removeObserver: 以及 postNotificationName:

 [[NSNotificationCenter defaultCenter] addObserver:self @selector(gotData)
                                              name:FOO_FETCH_SUCCESS object: baz];

所以 @"foobarFetchSuccess" 字符串到处使用。像他一样的人还有很多。 那么声明一次字符串并在任何地方使用它的最佳方法是什么?

In trying to de-couple my model from the view controllers that display fetched data, when an asynchronous fetch completes, I post an NSNotification.

 [[NSNotificationCenter defaultCenter] postNotificationName:@"foobarFetchSuccess" object: foo];

I've gotten into the habit of using:

 #define FOO_FETCH_SUCCESS  @"foobarFetchSuccess"

in a common header file and then using it for the addObserver: and removeObserver: as well as the postNotificationName:

 [[NSNotificationCenter defaultCenter] addObserver:self @selector(gotData)
                                              name:FOO_FETCH_SUCCESS object: baz];

So @"foobarFetchSuccess" string is used all over the place. And there are many more just like him.
So what's the best way to declare a string once and use it everywhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泅人 2024-11-09 17:54:33

这并不完全遵循苹果建议的格式,也没有直接回答你的问题,但我想我应该分享这些方便的花花公子的文本宏,我用它们来在制作通知和按键名称时节省自己的打字时间。您可以为它们分配键盘快捷键,输入并选择 [Did|Will] + [UniquePartOfName] 段,然后点击快捷键以生成变量及其值。如果您在特定类的标头中定义这些字符串,您也可以使用 $(FILENAMEASIDENTIFIER) 代替 $(PROJECTNAME),并且会 符合建议。

//MARK: Notification strings
    { /*
       * Use the selection to make the name and string for a Notification.
       * The string has a prefix placeholder, defaulting to the project name.
       */
      Identifier = objc.notestring;
      BasedOn = objc;
      IsMenuItem = YES;
      Name = "Notification Name From Selection";
      TextString = "<#$(PROJECTNAME)#><#!notename!#>Notification = @\"<#$(PROJECTNAME)#><#!notename!#>Notification\";";
      CompletionPrefix = notestring;
    },
    { /*
       * Insert placeholders to make the name and string for a Notification.
       * This is for use without a selection, and so "only" activates at the 
       * beginning of the line.
       */
      Identifier = objc.notestring.bol;
      BasedOn = objc.notestring;
      IsMenuItem = YES;
      Name = "Notification Name From Selection";
      OnlyAtBOL = YES;
      CompletionPrefix = notestring;
    },

//MARK: Default Key strings
    { /*
       * Convert the selection into a name and string for use in the User 
       * Defaults system. The string has a placeholder for a prefix, which
       * defaults to the project name.
       */
      Identifier = objc.defaultskeystring;
      BasedOn = objc;
      IsMenuItem = YES;
      Name = "UserDefaults Key From Selection";
      OnlyAtBOL = NO;
      TextString = "<#$(PROJECTNAME)#><#!keyname!#>Key = @\"<#$(PROJECTNAME)#><#!keyname!#>Key\";";
      CompletionPrefix = defaultskey;
    },
    { /*
       * Insert placeholders to make the name and string for a a key for the
       * User Defaults system. This is for use without a selection, and so 
       * "only" activates at the beginning of the line.
       */
      Identifier = objc.defaultskeystring.bol;
      BasedOn = objc.defaultskeystring;
      IsMenuItem = YES;
      OnlyAtBOL = YES;
      Name = "UserDefaults Key From Selection";
      CompletionPrefix = defaultskey;
    },

这些是 Xcode 3 宏。我知道 Xcode 4 中的宏系统不同(我还没有使用),但我相信转换很简单并且可以自动化。

This doesn't follow the Apple-suggested format exactly, nor does it directly answer your question, but I thought I'd share these handy-dandy text macros that I use to spare myself a little typing when making notification and key names. You can assign these a keyboard shortcut, type in and select the [Did|Will] + [UniquePartOfName] segment, then hit the shortcut to produce the variable and its value. You could also use $(FILENAMEASIDENTIFIER) instead of $(PROJECTNAME) if you were defining these strings in the header of a particular class, and that would conform to the suggestion.

//MARK: Notification strings
    { /*
       * Use the selection to make the name and string for a Notification.
       * The string has a prefix placeholder, defaulting to the project name.
       */
      Identifier = objc.notestring;
      BasedOn = objc;
      IsMenuItem = YES;
      Name = "Notification Name From Selection";
      TextString = "<#$(PROJECTNAME)#><#!notename!#>Notification = @\"<#$(PROJECTNAME)#><#!notename!#>Notification\";";
      CompletionPrefix = notestring;
    },
    { /*
       * Insert placeholders to make the name and string for a Notification.
       * This is for use without a selection, and so "only" activates at the 
       * beginning of the line.
       */
      Identifier = objc.notestring.bol;
      BasedOn = objc.notestring;
      IsMenuItem = YES;
      Name = "Notification Name From Selection";
      OnlyAtBOL = YES;
      CompletionPrefix = notestring;
    },

//MARK: Default Key strings
    { /*
       * Convert the selection into a name and string for use in the User 
       * Defaults system. The string has a placeholder for a prefix, which
       * defaults to the project name.
       */
      Identifier = objc.defaultskeystring;
      BasedOn = objc;
      IsMenuItem = YES;
      Name = "UserDefaults Key From Selection";
      OnlyAtBOL = NO;
      TextString = "<#$(PROJECTNAME)#><#!keyname!#>Key = @\"<#$(PROJECTNAME)#><#!keyname!#>Key\";";
      CompletionPrefix = defaultskey;
    },
    { /*
       * Insert placeholders to make the name and string for a a key for the
       * User Defaults system. This is for use without a selection, and so 
       * "only" activates at the beginning of the line.
       */
      Identifier = objc.defaultskeystring.bol;
      BasedOn = objc.defaultskeystring;
      IsMenuItem = YES;
      OnlyAtBOL = YES;
      Name = "UserDefaults Key From Selection";
      CompletionPrefix = defaultskey;
    },

These are Xcode 3 macros. I know the macro system is different in Xcode 4 (which I'm not using yet), but I believe the conversion is simple and can be automated.

笨死的猪 2024-11-09 17:54:32

至于在项目中使用常量字符串,Stack Overflow 上还有另一个问题:Constants in Objective C。

至于命名通知,Cocoa 编码指南< /a> 建议如下:

通知由全局 NSString 对象标识,其名称的组成方式如下:

[关联类的名称] + [Did | Will] + [UniquePartOfName] + 通知

As for using constant strings in your project, there’s another question on Stack Overflow about that: Constants in Objective C.

As for naming notifications, Coding Guidelines for Cocoa suggests the following:

Notifications are identified by global NSString objects whose names are composed in this way:

[Name of associated class] + [Did | Will] + [UniquePartOfName] + Notification

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文