2011年11月3日木曜日

Sometimes Talk About The Past...

♪ 時には昔の話をしようか ♪

そんな気分のときってあると思います。 クラスメソッドの initialize を実装してみましたが2回呼ばれているようです。10.6 Xcode 3.2.6 の話。

If a particular class does not implement initialize, the initialize method of its superclass is invoked twice, once for the superclass and once for the non-implementing subclass. If you want to make sure that your class performs class-specific initializations only once, implement initialize as in the following example:

特定のクラスが initialize を実装していない場合、スーパークラスの initialize メソッドは2回呼出されます。スーパークラスが1回と initialize を実装していないサブクラスが1回です。クラスが一度だけのクラス固有の初期化を実行することを確実にするには、次の例のように初期化を実装します。

@implementation MyClass
+ (void)initialize {
    if ( self == [MyClass class] ) {
        /* put initialization code here */
    }
}

実際この例のように実装してみたら呼出しが1回になりました。2011-09-08 に改訂されている NSObject Class Reference での initialize ではこの記述がなかったので、10.7 Xcode 4 では変更されているのかも。

ところで
もともと NSUserDefaults を使って NSRegistrationDomein にアプリケーションの初期値を設定しようと思っていました。

To register the application’s default behavior, you get the application's shared instance of NSUserDefaults and register default values with it. A good place to do this is in the initialize method of the class that uses the default. The following example registers the value “YES” for the default named “DeleteBackup”.
User Defaults Programming Topics "Setting a Default in the NSRegistrationDomain"

アプリケーションのデフォルトの動作を登録するために、NSUserDefaults の共有インスタンスを取得し、デフォルト値を登録します。これを行うのに良い場所は、デフォルト値を使用するクラスの initialize メソッドです。次の例は DeleteBackup という名前で YES の値を登録します。

+ (void)initialize {
 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *appDefaults = [NSDictionary
        dictionaryWithObject:@"YES" forKey:@"DeleteBackup"];
 
    [defaults registerDefaults:appDefaults];
}

とありましたが、この Topics は 2011-10-12 で更新されて Preferences and Settings Programming Guide という名前になりました。そしてこの記述はなくなりました。

♪ 今でも 同じように 見果てぬ夢を描いて 走り続けているよね どこかで ♪

0 件のコメント:

コメントを投稿