Reputation: 475
I need to initialize a date to a known, fixed, point in time. Swift lacking date literals, I tried :
extension DateFormatter
{
convenience init(_ format: String)
{
self.init()
self.dateFormat = format
}
}
extension Date
{
init?(_ yyyyMMdd: String)
{
let formatter = DateFormatter("yyyyMMdd")
self = formatter.date(from: yyyyMMdd)
}
}
Unfortunately, I can't write self = ...
or return formatter.date...
as part of the initializer.
However, I would very much like to write :
let date = Date("20120721")
How can I achieve this ?
Upvotes: 0
Views: 1482
Reputation: 539795
You can initialize a struct
by assigning a value to self
.
The problem in your case is that formatter.date(from:)
returns an optional, so you have to unwrap that first:
extension Date {
init?(yyMMdd: String) {
let formatter = DateFormatter("yyyyMMdd")
guard let date = formatter.date(from: yyMMdd) else {
return nil
}
self = date
}
}
You can even initialize a date from a string literal by
adopting the ExpressibleByStringLiteral
protocol:
extension Date: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
let formatter = DateFormatter("yyyyMMdd")
self = formatter.date(from: value)!
}
public init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
public init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
}
let date: Date = "20120721"
Note however that this would crash at runtime for invalid dates.
Two more remarks:
Locale(identifier: "en_US_POSIX")
to allow parsing the date string independent of
the user's locale settings.TimeZone(secondsFromGMT: 0)
.Upvotes: 6