{ 完整節目筆記 https://weakself.dev/episodes/26 }
為了讓節目內容兼具技術與閒聊,我們討論著 iOS 開發時遇到的坑。這集就由 13 先講與 DateFormatter 的不解之緣。
第一次:NSDateFormatter init 的坑
以前官方文件有寫說 [[NSDateFormatter alloc]init]
是 CPU expansive task,不過現在這句話找不到了。只有在 StackOverflow 上還有記錄。
論 iOS App Performance Optimization Q. 如何大幅提升 compile time 效率? A. 買最新的 Apple 電腦 Q. 如何大幅提升 runtime 效率? A. 等使用者買最新的 Apple 裝置 結論: 人們說「能靠花錢可以解決的都不是難事」,是不是沒有很難?🤪🤪🤪 @ethanhuang13
第二次:DateFormatter 的 Calendar 坑
If unspecified, the logical calendar for the current user is used. - DateFormatter.calendar
如果沒有指定的話, DateFormatter
就會使用 系統設定 > 一般 > 語言與地區 > 曆法
上的設定,除了西曆以外,還有日本曆、佛曆可以選。
Calendar 選 Gregorian 跟 ISO8061 時,yearForWeekOfYear 算法不一樣。
第三次:DateFormatter 真的炸掉了
String
轉 Date
是 nil
,奇怪咧!?
首先排除可能性:
App 刪掉重裝過
iOS 升級到最新版
系統的曆法設定是西曆,或是
DateFormatter
有指定calendar
系統時間沒有亂調
DateFormatter
不知什麼原因炸掉了
最後處理方法:去找使用者借裝置 debug
喬喬 Bonus:寫 Android 也懷疑人生
喬喬經驗分享
找使用者 debug
ISO8601DateFormatter
如果要使用 ISO8601 Date 的話,iOS 10+ 推薦使用 ISO8601DateFormatter。
波肥 Bonus: 也是 DateFormatter
DateFormatter 的 threading issue
How iOS alloc and free
Thread-Safe 的時光總是特別短暫
更多 weak self
Twitter @weak_self
Share this post