缓存 NSDateFormatter
为什么要缓存 NSDateFormatter
?
Creating a date formatter is not a cheap operation. If you are likely to use a formatter frequently, it is typically more efficient to cache a single instance than to create and dispose of multiple instances. One approach is to use a static variable. developer.apple.com
思路: 利用 NSCache
, 以 stringFormatter+NSLocale的localeIdentifier 为key缓存 NSDateFormatter
. 当UIApplicationDidReceiveMemoryWarningNotification
或 NSCurrentLocaleDidChangeNotification
释放 NSCache
缓存的对象.
代码参考code, 核心实现代码如下:
- (NSDateFormatter *)dateFormatterWithFormat:(NSString *)format andLocale:(NSLocale *)locale {
@synchronized(self) {
NSString *key = [NSString stringWithFormat:@"%@|%@", format, locale.localeIdentifier];
NSDateFormatter *dateFormatter = [loadedDataFormatters objectForKey:key];
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = format;
dateFormatter.locale = locale;
[loadedDataFormatters setObject:dateFormatter forKey:key];
}
return dateFormatter;
}
}