[英]Function with Enumeration Parameter (and Default value)
我想創建一個名為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。 我做了一些閱讀,以了解其工作原理,但無法弄清楚代碼。
誰能闡明如何使它工作?
謝謝你的幫助!
抱歉,我沒有提出這個要求。 我應該再等一會兒。 為了希望幫助別人,只是提醒一下枚舉的“原始”方面:
將此添加到我的函數簽名中有助於:
CurrencyFormatType.Raw = 1
謝謝!
或者,您可以這樣做:
func toCurrencyString(currencyFormat: CurrencyFormatType = CurrencyFormatType.Standard) -> String
{
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.