Skip to content

Instantly share code, notes, and snippets.

@Abizern
Last active August 29, 2015 14:19
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 Abizern/8a4b1bce8d970b8f0f39 to your computer and use it in GitHub Desktop.
Save Abizern/8a4b1bce8d970b8f0f39 to your computer and use it in GitHub Desktop.
You don't need to declare a custom class to have a singleton formatter, just use a global lazy var.
var formatter: NSNumberFormatter = {
let f = NSNumberFormatter()
f.locale = NSLocale.currentLocale()
f.maximumFractionDigits = 2
f.minimumFractionDigits = 2
f.alwaysShowsDecimalSeparator = true
f.numberStyle = .CurrencyStyle
return f
}()
let a = formatter
let b = formatter
a === b // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment