12. 缓存 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.
思路: 利用 NSCache
, 以 stringFormatter+NSLocale
的 localeIdentifier
为 key
缓存 NSDateFormatter
. 当UIApplicationDidReceiveMemoryWarningNotification
或 NSCurrentLocaleDidChangeNotification
释放 NSCache
缓存的对象.
代码参考 BTNSDateFormatterFactory.m, 核心实现代码如下:
Last updated