我在看下面的苹果示例源代码时:使用Objective-C中static关键字定义一个变量缓存
/*
Cache the formatter. Normally you would use one of the date formatter styles (such as NSDateFormatterShortStyle), but here we want a specific format that excludes seconds.
*/
static NSDateFormatter *dateFormatter = nil;
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"h:mm a"];
}
试图找出:
为什么使用静态关键词?
如果在每次调用方法时将其设置为nil,它将如何等于缓存变量。
的代码是从实施例4中Tableview Suite demo
我知道这是老问题,但不知道这个静态关键字与ARC的关系的影响? – codejunkie 2012-01-13 21:25:58
@ codejunkie静态存储在与堆不同的位置。 ARC关心保留和释放堆内存,以便值可以保持超出堆栈帧。因此,我怀疑ARC对静态变量有什么影响。 – smileBot 2014-03-14 20:32:11