作者:Joe,原文鏈接坚嗜,原文日期:2016-09-20
譯者:Cwift夯膀;校對(duì):walkingway;定稿:CMB
當(dāng)你在想要 大規(guī)模重命名 時(shí)苍蔬,一個(gè)附帶的挑戰(zhàn)就是要確保所有相關(guān)的文檔都必須同步更新诱建。比如,截至到 2016 年 9 月 20 日碟绑,DateFormatter 的文檔依舊沒有與版本統(tǒng)一俺猿,引用的是 Swift 2.3 風(fēng)格的 API(譯者注:現(xiàn)在是 2017年,文檔依舊沒有更新...)格仲。隨著時(shí)間的推移押袍,這些疏漏毫無疑問都會(huì)被糾正,這里是一些使用 Date
以及 DateFormatter
實(shí)現(xiàn)的日期格式化的示例抓狭。
官方文檔中當(dāng)前的示例如下:
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .mediumStyle
dateFormatter.timeStyle = .noStyle
let date = Date(timeIntervalSinceReferenceDate: 118800)
// US English Locale (en_US)
dateFormatter.locale = Locale(localeIdentifier: "en_US")
NSLog("%@", dateFormatter.stringFromDate(date)) // Jan 2, 2001
// French Locale (fr_FR)
dateFormatter.locale = Locale(localeIdentifier: "fr_FR")
NSLog("%@", dateFormatter.stringFromDate(date)) // 2 janv. 2001
// Japanese Locale (ja_JP)
dateFormatter.locale = Locale(localeIdentifier: "ja_JP")
NSLog("%@", dateFormatter.stringFromDate(date)) // 2001/01/02
在 Swift 3.0 中變?yōu)椋?/p>
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
注意 .mediumStyle
被簡(jiǎn)化為 .medium
伯病。這種簡(jiǎn)化符合規(guī)則,我們知道類型是 DateFormatter.Style
所以沒有理由重復(fù) Style 一詞否过。使用 .none
替換 .noStyle
也是同理午笛。
現(xiàn)在看看設(shè)置格式化器的環(huán)境時(shí)發(fā)生的改動(dòng):
// US English Locale (en_US)
dateFormatter.locale = Locale(identifier: "en_US")
print(dateFormatter.string(from:date)) // Jan 2, 2001
// French Locale (fr_FR)
dateFormatter.locale = Locale(identifier: "fr_FR")
print(dateFormatter.string(from:date)) // 2 janv. 2001
// Japanese Locale (ja_JP)
dateFormatter.locale = Locale(identifier: "ja_JP")
print(dateFormatter.string(from:date)) // 2001/01/02
再一次,我們看到了從 Locale(localeIdentifier:)
到 Locale(identifier:)
的簡(jiǎn)化苗桂。減少了類型名及其引用药磺。類似地,DateFormatter
的 stringFromDate
方法已經(jīng)被簡(jiǎn)化為 string(from:)
煤伟,完整的方法簽名是 string(from:Date)
癌佩。理解這種模式了嗎木缝?
繼續(xù)用一個(gè) String
描述的信息生成一個(gè) Date
對(duì)象,蘋果的文檔展示了這樣的示例:
let RFC3339DateFormatter = DateFormatter()
RFC3339DateFormatter.locale = Locale(localeIdentifier: "en_US_POSIX")
RFC3339DateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
RFC3339DateFormatter.timeZone = TimeZone(forSecondsFromGMT: 0)
let string = "1996-12-19T16:39:57-08:00"
let date = RFC3339DateFormatter.dateFromString(string)
為了減少啰嗦和不必要的詞匯围辙,得到了下面的寫法:
let RFC3339DateFormatter = DateFormatter()
RFC3339DateFormatter.locale = Locale(identifier: "en_US_POSIX")
RFC3339DateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
RFC3339DateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
let string = "1996-12-19T16:39:57-08:00"
let date = RFC3339DateFormatter.date(from:string)
TimeZone
構(gòu)造器中刪除了無關(guān)的三個(gè)字符(for)我碟,而且正如期望, dateFromString
方法變成了 date(from:)
姚建。
經(jīng)驗(yàn)法則
從 Swift 2 轉(zhuǎn)換到 Swift 3 時(shí)矫俺,一個(gè)通用法則是:去除多余的單詞。如果你之前習(xí)慣了寫 formatter.timeStyle = .fullStyle
掸冤,現(xiàn)在你要習(xí)慣 formatter.timeStyle = .full
的寫法厘托。如果你看到了 someTypeFromAnotherType()
這樣的寫法,它可能已經(jīng)被替換為 someType(from:AnotherType)
稿湿。說一下我的體驗(yàn)铅匹,在使用 Swift 3 幾個(gè)月時(shí)間后,再回到 Swift 2 會(huì)感覺命名過于冗長(zhǎng)饺藤,這還是在我喜歡這種詳細(xì)自說明語言風(fēng)格的前提下包斑。不過一旦你學(xué)會(huì)了 Swift,你會(huì)擁抱海明威而避開托爾斯泰策精。
愉快地 Swift 吧舰始!
本文由 SwiftGG 翻譯組翻譯,已經(jīng)獲得作者翻譯授權(quán)咽袜,最新文章請(qǐng)?jiān)L問 http://swift.gg。