如何删除可选字
public func getCurrencySymbolFromCurrencyCode(currencyCode: String) -> String {
// let currencyCode: String = "EUR"
let locale: NSLocale = NSLocale(localeIdentifier: currencyCode)
let currencySymbol: String = "\(locale.displayNameForKey(NSLocaleCurrencySymbol, value: currencyCode))"
print("Currency Symbol : \(currencySymbol)")
return currencySymbol
}
以上代码产生如下:
Optional("$")
我的问题是为什么Optional("$")
存在$
,如何删除可选内容并打印String
答案 0 :(得分:3)
//用你的代码替换你的代码你不是强行打开符号
public func getCurrencySymbolFromCurrencyCode(currencyCode: String) -> String! {
// let currencyCode: String = "EUR"
let locale: NSLocale = NSLocale(localeIdentifier: currencyCode)
let symbol = locale.displayNameForKey(NSLocaleCurrencySymbol, value: currencyCode)!
let currencySymbol: String = "\(symbol)"
print("Currency Symbol : \(currencySymbol)")
return currencySymbol
}