func Function(_ currency: Currency, _ amount: Int) -> String {
func unsignInt( amou: Int) -> String {
return String(-amou)
}
let curArray = [
".rub": " ₽;",
".eur": " €;",
".usd": " $;"
]
var amountRes = ""
amount < 0 ? (amountRes = "(" + unsignInt(amou: amount) + ")") : (amountRes = String(amount))
for (curCode, prnCode) in curArray{
if curCode as AnyObject === currency as AnyObject {
return amountRes + prnCode
}
}
return "0"
}
此函数接受货币和整数形式的货币。它应该返回一个包含金额和货币符号的字符串。负数必须显示在括号中,并且不带减号。但这个功能不起作用,请帮助某人找出
您的代码很奇怪。我会写的更像这样:
func amount(_ currency: Currency, _ amount: Int) -> String {
let currancyDict = [
".rub": " ₽;",
".eur": " €;",
".usd": " $;"
]
let amountString: String
if amount < 0 {
amountString = "((abs(amount)))"
} else {
amountString = "(amount)"
}
if let abbreviation = currancyDict[currency.string] {
return amountString + abbreviation
} else {
return "0"
}
}
为了实现这一点,您需要在Currency
类型上实现一个计算属性string
,该属性在currencyDict
的键中使用时返回字符串。
我甚至会将currencyDict
放入Currency
类型中,并在Currency
类型中添加一个方法,该方法接受一个数量整数并返回字符串。