views:

165

answers:

1

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.

Please need your guidance on it.

Thanks in advance for sharing your great knowledge.

Sagar

+3  A: 

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 the initialize 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 an initialize 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.

KennyTM