首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSNotifications名称最佳实践

NSNotifications名称最佳实践
EN

Stack Overflow用户
提问于 2011-04-21 06:37:04
回答 2查看 9.8K关注 0票数 26

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

代码语言:javascript
复制
 [[NSNotificationCenter defaultCenter] postNotificationName:@"foobarFetchSuccess" object: foo];

我已经养成了使用的习惯:

代码语言:javascript
复制
 #define FOO_FETCH_SUCCESS  @"foobarFetchSuccess"

在一个公共的头文件中,然后将其用于addObserver:和removeObserver:以及postNotificationName:

代码语言:javascript
复制
 [[NSNotificationCenter defaultCenter] addObserver:self @selector(gotData)
                                              name:FOO_FETCH_SUCCESS object: baz];

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-04-21 06:48:25

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

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

通知由全局NSString对象标识,这些对象的名称组成如下:

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

票数 54
EN

Stack Overflow用户

发布于 2011-04-21 07:46:00

这并不完全遵循Apple建议的格式,也没有直接回答你的问题,但我想我应该分享这些方便的、花哨的文本宏,我使用这些宏来省去自己在创建通知和关键名称时的一些键入。您可以为它们分配一个键盘快捷键,键入并选择[Did|Will] + [UniquePartOfName]段,然后点击快捷键以生成变量及其值。如果在特定类的头中定义这些字符串,也可以使用$(FILENAMEASIDENTIFIER)而不是$(PROJECTNAME),这将符合建议。

代码语言:javascript
复制
//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;
    },

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5737450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档