iPhone Development

Just another iPhone Blog

Quick Tip : Initializing static variables of a class in Objective C

Initializes the receiver before it’s used (before it receives its first message).

 

+ (void)initialize


Discussion The runtime sends initialize to each class in a program exactly one time just before the class, or any class that inherits from it, is sent its first message from within the program. (Thus the method may never be invoked if the class is not used.) The runtime sends the initialize message to classes in a thread-safe manner. Superclasses receive this message before their subclasses.


[Reference : NSObject Class Reference]

You can place all your static member variable initialization code in this method as it guarantees it would be called only once BEFORE any method in that Class is invoked through any of its objects.

Happy Coding 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: