I'm trying to confirm whether static variable initialization in Objective-C works the same as it does for C++. Specifically do static variables have the possibility of being instantiated/created before main() is call开发者_运维技巧ed?
There's no concept of instantiation of static variables in Objective-C proper. e.g.
// file level
NSMutableArray* foo = [[NSMutableArray alloc] init];
is not allowed. It's the same as in C: you can only initialize static variables with constants. For Objective-C objects, that means nil
or literal strings.
In Objective-C++, you can do that, and they are called before main()
. It just follows C++'s part of the rules of Objective-C++.
When you want to initialize objects associated to a class in Objective-C, you use +initialize
or +load
. See this nice blog article.
精彩评论