外包项目网站北京做网站的公司排行
news/
2025/9/23 12:36:53/
文章来源:
外包项目网站,北京做网站的公司排行,wordpress安装到网站吗,品牌网站如何做为什么要优化NSDateFormatter#xff1f;首先#xff0c;过度的创建NSDateFormatter用于NSDate与NSString之间转换#xff0c;会导致App卡顿#xff0c;打开Profile工具查一下性能#xff0c;你会发现这种操作占CPU比例是非常高的。据官方说法#xff0c;创建NSDateForma… 为什么要优化NSDateFormatter首先过度的创建NSDateFormatter用于NSDate与NSString之间转换会导致App卡顿打开Profile工具查一下性能你会发现这种操作占CPU比例是非常高的。据官方说法创建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优化方式有哪些a.延迟转换即只有在UI需要使用转换结果时在进行转换。b.Cache in Memory根据NSDateFormatter线程安全性不同的iOS系统版本内存缓存如下prior to iOS 7如果直接采用静态变量进行存储那么可能就会存在线程安全问题在iOS 7之前NSDateFormatter是非线程安全的因此可能就会有两条或以上的线程同时访问同一个日期格式化对象从而导致App崩溃。 (NSDateFormatter *)cachedDateFormatter {NSMutableDictionary *threadDictionary [[NSThread currentThread] threadDictionary];NSDateFormatter *dateFormatter [threadDictionary objectForKey:cachedDateFormatter];if (!dateFormatter) {dateFormatter [[NSDateFormatter alloc] init];[dateFormatter setLocale:[NSLocale currentLocale]];[dateFormatter setDateFormat: YYYY-MM-dd HH:mm:ss];[threadDictionary setObject:dateFormatter forKey:cachedDateFormatter];}return dateFormatter;}iOS 7 or later在iOS 7、macOS 10.9及以上系统版本NSDateFormatter都是线程安全的因此我们无需担心日期格式化对象在使用过程中被另外一条线程给修改为了提高性能我们还可以在上述代码块中进行简化除去冗余部分。static NSDateFormatter *cachedDateFormatter nil; (NSDateFormatter *)cachedDateFormatter {NSMutableDictionary// If the date formatters arent already set up, create them and cache them for reuse.if (!dateFormatter) {dateFormatter [[NSDateFormatter alloc] init];[dateFormatter setLocale:[NSLocale currentLocale]];[dateFormatter setDateFormat: YYYY-MM-dd HH:mm:ss];}return dateFormatter;}如果缓存了日期格式化或者是其他依赖于current locale的对象那么我们应该监听NSCurrentLocaleDidChangeNotification通知当current locale变化时及时更新被缓存的日期格式化对象。In theory you could use an auto-updating locale (autoupdatingCurrentLocale) to create a locale that automatically accounts for changes in the user’s locale settings. In practice this currently does not work with date formatters.Apple Threading Programming Guidec.利用标准C语言库如果时间日期格式是固定的我们可以采用C语言中的strptime函数这样更加简单高效。- (NSDate *) easyDateFormatter{time_t t;struct tm tm;char *iso8601 2016-09-18;strptime(iso8601, %Y-%m-%d, tm);tm.tm_isdst -1;tm.tm_hour 0;//当tm结构体中的tm.tm_hour为负数会导致mktime(tm)计算错误/**//NSString *iso8601String 2016-09-18T17:30:0808:00;//%Y-%m-%d [iso8601String cStringUsingEncoding:NSUTF8StringEncoding]{tm_sec 0tm_min 0tm_hour 0tm_mday 18tm_mon 9tm_year 116tm_wday 2tm_yday 291tm_isdst 0tm_gmtoff 28800tm_zone 0x00007fd9b600c31c CST}ISO8601时间格式2004-05-03T17:30:0808:00 参考Wikipedia*/t mktime(tm);//http://pubs.opengroup.org/onlinepubs/9699919799/functions/mktime.html//secondsFromGMT: The current difference in seconds between the receiver and Greenwich Mean Time.return [NSDate dateWithTimeIntervalSince1970:t [[NSTimeZone localTimeZone] secondsFromGMT]];} 转载于:https://blog.51cto.com/13064681/1943390
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/912558.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!