具有枚举参数的函数(和默认值)

时间:2014-08-27 02:58:08

标签: ios ios7 swift ios8

我想创建一个名为toCurrencyString的扩展方法函数,该函数有一个参数实际上是CurrencyFormatType类型的整数枚举。枚举和代码将是:

enum CurrencyFormatType: Int {
    /// Formats a standard currency string (localized) such as $45.35 or *45,00
    case Standard = 1,

    ///Rounded currency format (rounds up last decimals and does not return any decimals in string)
    Rounded,

    ///Will not include the thousands separator (, or .) in a string. Retains localized currency symbol.
    WithoutThousandsSeparator

}


func toCurrencyString(currencyFormat: CurrencyFormatType = 1) -> String
{
  ..my switch code w/ default to the first option if nothing passed in
}

我想这样做,这样我就可以在NSDecimalNumbers上调用.toCurrencyString而不必传递可选参数(大部分时间我都不需要它),但是几次我我想使用枚举来选择其他选项。

我抱怨告诉我,我的枚举不符合IntegerLiteralConvertible。我做了一些关于这将如何工作的阅读,但无法弄清楚代码。

有人能说明如何让这个工作吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我道歉,我一问它就比我想出来的还要多。我应该等一会儿。 希望能帮助他人,只需提醒一下enum的“原始”方面:

将此添加到我的函数签名中有帮助:

CurrencyFormatType.Raw = 1

谢谢!

或者你可以做到:

func toCurrencyString(currencyFormat: CurrencyFormatType = CurrencyFormatType.Standard) -> String
{
...