+ load
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:
あなたが複数回実行されることから身を守りたいならば、あなたはこれらの行に沿ってあなたの実装を組織立てることができます:
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.