我在看下面的蘋果示例源代碼時:使用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