Type Method 型メソッド

initialize()

Initializes the class before it receives its first message. クラスを、それがそれの最初のメッセージを受け取る前に初期化します。

Declaration 宣言

class func initialize()

Discussion 解説

The runtime sends initialize() to each class in a program just before the class, or any class that inherits from it, is sent its first message from within the program. Superclasses receive this message before their subclasses. スーパークラスは、このメッセージをそれらのサブクラスの前に受け取ります。

The runtime sends the initialize() message to classes in a thread-safe manner. That is, initialize() is run by the first thread to send a message to a class, and any other thread that tries to send a message to that class will block until initialize() completes.

The superclass implementation may be called multiple times if subclasses do not implement initialize()—the runtime will call the inherited implementation—or if subclasses explicitly call [super initialize]. If you want to protect yourself from being run multiple times, you can structure your implementation along these lines: あなたが複数回実行されることから身を守りたいならば、あなたはこれらの行に沿ってあなたの実装を組織立てることができます:


+ (void)initialize {
  if (self == [ClassName self]) {
    // ... do the initialization ...
  }
}

Because initialize() is called in a blocking manner, it’s important to limit method implementations to the minimum amount of work necessary possible. Specifically, any code that takes locks that might be required by other classes in their initialize() methods is liable to lead to deadlocks. Therefore, you should not rely on initialize() for complex initialization, and should instead limit it to straightforward, class local initialization.

Special Considerations 特別な注意事項

initialize() is invoked only once per class. If you want to perform independent initialization for the class and for categories of the class, you should implement load() methods.

See Also 参照

Initializing a Class クラスの初期化

Related Documentation 関連文書