Let's say that I have something like this:
extension NSNumber{
func toLocalCurrency(fractDigits:Int = 2)->String{
let formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
let userSettings:UserInfo? = UserInfo.first(sortDescriptors: nil, context: AERecord.defaultContext) as? UserInfo
if let code = userSettings?.currency.name_short {
formatter.currencyCode = code
}
formatter.maximumFractionDigits = fractDigits
return formatter.stringFromNumber(self)!
}
func toLocalCurrencyWithoutFractionDigits()->String{
return self.toLocalCurrency(fractDigits: 0)
}
}
I want this to be supported as many types of swift / mac number as possible, for example. CGFLoat NSNumber Int Float etc. But I don’t want to repeat myself (copy paste and expand everything) or quit everywhere, I want to use this function.
I tried to extend protocols like FloatLiteralType / Convertible, but also needed casting. This “should” is possible to expand the basic types in a more convenient way.
I also thought about global functions, but they are less clear and feel more hacked.
Is there a good way to achieve this fast?
source
share