0

Will 'dateFormatter' evaluate once, like lazy ?

class Birthday {

  private static var dateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.locale = Locale(identifier: "en_US_POSIX")
    formatter.dateFormat = "d MMMM yyyy"
    return formatter
  }()

  private let date: Date

  init(date: Date) {
    self.date = date
  }

}

asdf

  • 1
    Yes, compare http://stackoverflow.com/questions/24024549/using-a-dispatch-once-singleton-model-in-swift. E.g. the quote in this http://stackoverflow.com/a/24024762/1187415 answer: "*The lazy initializer for a global variable (**also for static members of structs and enums**) is run the first time that global is accessed,*" – Martin R Mar 21 '17 at 16:30
  • See also https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html#//apple_ref/doc/uid/TP40014216-CH7-ID14: *"In Swift, you can simply use a static type property, which is guaranteed to be lazily initialized only once, even when accessed across multiple threads simultaneously"* – Martin R Mar 21 '17 at 16:35

0 Answers0