Skip to content

Instantly share code, notes, and snippets.

@daehn
Last active August 29, 2015 14:26
Show Gist options
  • Save daehn/97430090ddf034bf8963 to your computer and use it in GitHub Desktop.
Save daehn/97430090ddf034bf8963 to your computer and use it in GitHub Desktop.
Struct containing the displayNames and codes for the common ISO currency codes.
struct Currency {
let code, displayName: String
init?(code: String?) {
if let code = code,
displayName = NSLocale.systemLocale().displayNameForKey(NSLocaleCurrencyCode, value:code) {
self.code = code
self.displayName = displayName
} else {
return nil
}
}
}
struct CurrencyDataSource {
let currencies: [Currency] = {
// The first `map` can be replaced with `flatMap` in Swift 2.0
// to remove the `filter` and `map` calls afterwards
NSLocale.commonISOCurrencyCodes().map {
return Currency(code: $0 as? String)
}.filter { $0 != nil }.map { $0! }
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment