26

フェッチされたデータを表示するビュー コントローラーからモデルを分離しようとして、非同期フェッチが完了すると、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" 文字列がいたるところで使用されています。そして、彼のような人は他にもたくさんいます。では、一度文字列を宣言して、それをどこでも使用する最良の方法は何でしょうか?

4

2 に答える 2

54

プロジェクトで定数文字列を使用することに関しては、Stack Overflow に関する別の質問があります: Constants in Objective C

命名通知に関しては、Cocoa のコーディング ガイドラインでは次のことを提案しています。

通知は、名前が次のように構成されたグローバル NSString オブジェクトによって識別されます。

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

于 2011-04-20T22:48:25.103 に答える
3

これは、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 (まだ使用していません) ではマクロ システムが異なることは知っていますが、変換は簡単で自動化できると思います。

于 2011-04-20T23:46:00.373 に答える