我从here找到了一些示例代码。
static UIImage *backgroundImageDepressed;
/**
*
*/
@implementation DecimalPointButton
+ (void) initialize {
backgroundImageDepressed = [[UIImage imageNamed:@"decimalKeyDownBackground.png"] retain];
}
是这样的 -
+(void) initialize
方法初始化 Objective C 中类(接口)的静态变量吗?我以前从未见过这个。
此
+initialize
方法在The Objective-C Runtime中进行了描述。
例如,当调用运行时系统在类接收任何其他消息之前
以及其超类收到initialize
消息之后,向每个类对象发送initialize
消息。这使类有机会在使用之前设置其运行时环境。如果不需要初始化,就不需要写initialize
方法来响应消息。
[DecimalPointButton alloc]
时,运行时将检查
[DecimalPointButton initialize]
是否已被调用。如果没有,班级将会
+initialize
。这确保了
backgroundImageDepressed
图像在构造DecimalPointButton的任何实例之前准备就绪。