6

Say you have a date formatter that is global

let df:DateFormatter = DateFormatter()

Ideally it would be good to do the .dateFormat setup at the same global initialization time - sort of like this

let df:DateFormatter = DateFormatter().dateFormat = "yyyy-MM-dd"

But there's no alternative initialization for DateFormatter (there's nothing like this DateFormatter(withDateFormat: "yyyy-MM-dd") )

Really, is there a Swift solution to this? Is there syntax for a code block outside of any class, which, runs early before anything and in which you can setup things of that nature?

(Note - I am entirely aware of alternative approaches such as singletons, extension, etc: this is a Swift structure question, thanks.)

Hamish
  • 78,605
  • 19
  • 187
  • 280
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • http://stackoverflow.com/a/28347285/2303865 – Leo Dabus Feb 22 '17 at 16:47
  • Note that it is preferable to use date and time style (short, medium, long or full) which will be localized to the user instead of forcing a dateFormat if you intend to display it to the user. – Leo Dabus Feb 22 '17 at 16:49
  • The first link shows exactly what vadian answered but declared as static inside Date – Leo Dabus Feb 22 '17 at 16:56

3 Answers3

11

Use a closure to make your settings

let df : DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    return formatter 
}()

However you are discouraged from using uncapsulated global variables.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • To be absolutely totally clear: that closure would just run the once (I guess, literally when iOS launches the app). There are no circumstances where it would run again when df is used? (I just did some quick testing - but I though I'd ask.) Thanks! – Fattie Feb 22 '17 at 17:19
  • Yes, it runs once. – vadian Feb 22 '17 at 17:20
  • A simple clear question with a simple clear answer is so rare on SO! :) Thanks again – Fattie Feb 22 '17 at 17:21
3

You are free to implement a convenience initializer in an extension, like this:

extension DateFormatter {
    convenience init(dateFormat: String) {
       self.init()
       self.dateFormat = dateFormat
    }
}

Usage:

let dateFormatter = DateFormatter(dateFormat: "yyyy-MM-dd")
dateFormatter.date(from: "2017-02-22")
Ivan
  • 330
  • 2
  • 9
  • from the question: (Note - I am entirely aware of alternative approaches such as singletons, extension, etc: this is a Swift structure question, thanks.) – Fattie Feb 22 '17 at 16:56
-1

You can create your own custom class for that like:

class myDateformatter : DateFormatter {

    override init() {
        super.init()
        self.dateFormat = "yyyy-MM-dd"
        let timeZone = TimeZone(identifier: "UTC")
        self.timeZone = timeZone
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
  • from the question: (Note - I am entirely aware of alternative approaches such as singletons, extension, etc: this is a Swift structure question, thanks.) – Fattie Feb 22 '17 at 16:55