Skip to content

Instantly share code, notes, and snippets.

@BasThomas
Last active December 2, 2017 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BasThomas/9b95956c991e2c2c23c94715fab027f4 to your computer and use it in GitHub Desktop.
Save BasThomas/9b95956c991e2c2c23c94715fab027f4 to your computer and use it in GitHub Desktop.
import UIKit
class VC: UIViewController {
func formatter() -> DateFormatter {
struct Formatter {
static let f: DateFormatter = {
let f = DateFormatter()
f.dateStyle = .medium
print("I am called just once")
return f
}()
}
return Formatter.f
}
}
VC().formatter() === VC().formatter() // true, and printing just once.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment