开发者

Initialization order of static variables

开发者 https://www.devze.com 2023-01-14 10:50 出处:网络
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 befor

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.

0

精彩评论

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