开发者

Objective-C Class Constructors

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开发者_如何学C 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜