12. 缓存 NSDateFormatter
- (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;
}
}Last updated
Was this helpful?