views:

48

answers:

1

In my Objective-C project I have a Country class (NSManagedObject subclass). When comparing a country to another its important for me to know the total world population. I'm doing this operation a lot and it's getting expensive. I decided to put the population in a static variable of Country. However I want to initialize this value before ever making an instance of Country.

Apparently C# has something called Class Constructors which get called before you ever init an instance of the same class. This would be a perfect time for me to set the world population variable. But I can't figure out a way to do something similar with Objective-C. Is there even a way to do that?

I'm open to other approaches of this as well. Thanks, Rob

+3  A: 

You're probably looking for +initialize:

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.

Steve Madsen
Thank you, just found a link to a question with this suggestion too. Amazing what you find when you know what to look for!
rob5408