Lets say 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 that to support as most of swift/mac number types as possible eg. CGFLoat NSNumber Int Float etc. But i dont want to repeat myself (copy paste and extend everything) or cast everywhere i want to use that function.
I tried to extend protocols like FloatLiteralType/Convertible but needs also casting. It "should" be possible to extend basic types in a more convenient way..
I also thought of global functions but they are less discoverable and feel more hacky.
Is there a nice way to achieve this in swift?