【问题标题】:NSNotifications name best practiceNSNotifications 命名最佳实践
【发布时间】:2011-08-09 21:30:03
【问题描述】:

在尝试将我的模型与显示获取的数据的视图控制器分离时,当异步获取完成时,我发布了一个 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" 字符串被到处使用。还有更多像他一样的人。 那么声明一个字符串并在任何地方使用它的最佳方式是什么?

【问题讨论】:

    标签: objective-c c-preprocessor nsnotification


    【解决方案1】:

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

    关于命名通知,Coding Guidelines for Cocoa 建议如下:

    通知由全局 NSString 对象标识,其名称以这种方式组成:

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

    【讨论】:

    • 为了完整起见,观察选择器的命名约定呢?根据guideline,它与通知名称几乎相同,减去“通知”和类命名空间前缀。 eq: windowDidLoad 这准确吗?
    • @pix 您可以随意命名它们。但是,如果它是因为通知而调用的委托方法,那么您所说的适用 - 委托不会注册通知;相反,它会自动接收带有通知参数的相应消息。请注意,-windowDidLoad 与通知并不真正相关(它没有收到 NSNotification 参数),但命名约定确实相似。
    • 我对约定的不满是名称变得非常长并且整体可读性受到负面影响。
    【解决方案2】:

    这并没有完全遵循 Apple 建议的格式,也没有直接回答您的问题,但我想我会分享这些方便的花花公子文本宏,以便在制作通知和键时节省自己的一点打字时间名字。您可以为它们分配一个键盘快捷键,输入并选择[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(我还没有使用)中有所不同,但我相信转换很简单并且可以自动化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      • 2022-11-10
      • 2010-10-09
      • 2011-11-05
      • 2018-11-19
      • 2014-06-14
      • 2011-01-07
      相关资源
      最近更新 更多