I found some sample code from here.
static UIImage *backgroundImageDepressed;
/**
*
*/
@implementation DecimalPointButton
+ (void) initialize {
backgroundImageDepressed = [[UIImage imageNamed:@"decimalKeyDownBackground.png"] retain];
}
Is it something like this - +(void) initialize
method initialize static variables of a class ( interface ) in Objective C? I have never seen this before.
If you declare a static variable in a class, if you haven't initialized it, just like with instance variables compiler initializes these with default values in the default constructor. Yes, you can also initialize these values using the constructor.
All static members are always initialized before any class constructor is being called.
As static variables are initialized only once and are shared by all objects of a class, the static variables are never initialized by a constructor. Instead, the static variable should be explicitly initialized outside the class only once using the scope resolution operator (::).
The static variable is only initialized the first time when a function is called. In a static variable, the memory of a static variable is allocated. A global static variable is not accessible outside the program.
This +initialize
method is described in The Objective-C Programming Language.
The runtime system sends an
initialize
message to every class object before the class receives any other messages and after its superclass has received theinitialize
message. This gives the class a chance to set up its runtime environment before it’s used. If no initialization is required, you don’t need to write aninitialize
method to respond to the message.
For example, when [DecimalPointButton alloc]
is called, the runtime will check if [DecimalPointButton initialize]
has been called. If not, it will +initialize
the class. This ensure the backgroundImageDepressed
image is ready before any instances of DecimalPointButton are constructed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With