Swift如何删除可选的String字

时间:2016-08-19 07:12:55

标签: swift2

如何删除可选字

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

1 个答案:

答案 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
      }