Tags: Medium translation Information Another RFC wal note Environment developer
Joe, original link, original date: 2016-09-20
Translator: Cwift; proofreading: walkingway; final: CMB
When you want to rename at scale, a companion challenge is to ensure that all relevant documents must be updated synchronously. For example, as of September 20, 2016, Dateformatter's documentation is still not consistent with the version, citing the Swift 2.3-style API (translator Note: It is 2017, the document is still not updated ...) ）。 Over time, these omissions will undoubtedly be corrected, here are some examples of using
DateFormatter implementing date formatting.
The current examples in the official documentation are as follows:
Let Dateformatter = Dateformatter () dateformatter. Datestyle =. mediumstyledateformatter. Timestyle =. nostyle Let date = Date (timeintervalsincereferencedate:118800)US 中文版 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 ( Span class= "hljs-string" > "%@", dateformatter.stringfromdate (date)) //2001/01/02
In Swift 3.0, change to:
let dateFormatter = DateFormatter()dateFormatter.dateStyle = .mediumdateFormatter.timeStyle = .none
.mediumStyle is simplified to
.medium . This simplification conforms to the rules, and we know that the type is
DateFormatter.Style so there is no reason to repeat the word Style. The
.noStyle same is true with substitution.
Now look at the changes that occurred while setting up the formatter's environment:
US 中文版 Locale (en_US) dateformatter. Locale = locale (identifier: "en_US") print (Dateformatter.string (from :d ate)) //Jan 2, 2001 //French Locale (fr_fr) dateformatter< Span class= "hljs-selector-class" >.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
Once again, we see the simplification from the
Locale(identifier:) . The type name and its references are reduced. Similarly,
stringFromDate method has been simplified to a
string(from:) complete method signature
string(from:Date) . Do you understand this pattern?
String generate an object with a descriptive message
Date , and Apple's documentation shows an example:
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)
In order to reduce the wordy and unnecessary vocabulary, the following wording was obtained:
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)
Rule of thumb
TimeZoneThe constructor removes extraneous three characters (for), and as expected, the
dateFromString method becomes
A common rule for converting from Swift 2 to Swift 3 o'clock is to remove extra words. If you have been used to writing
formatter.timeStyle = .fullStyle , now you have to get used to
formatter.timeStyle = .full the wording. If you see
someTypeFromAnotherType() this notation, it may have been replaced by
someType(from:AnotherType) . Speaking of my experience, after using Swift 3 months, and then returning to Swift 2, I feel that the name is too lengthy, which I prefer in the context of a detailed self-explanatory language style. But once you've learned Swift, you'll embrace Hemingway and avoid Tolstoy.
This article is translated by Swiftgg translation Group, has obtained the author's translation authorization, the latest article please visit http://swift.gg.
Manipulating dates with Swift 3.0