【发布时间】:2019-06-17 10:16:58
【问题描述】:
我有这段代码可以从[String: Any] 中获取Double 数量并格式化这样的字符串
if let amount = details["amount"] as? Double
{
self.amountLbl.text = String(format: "%.2f", amount)
}
我正在尝试为此创建扩展
预期
//self.amountLbl.text = details.getInAmountFormat(str: "amount")
我的尝试
extension Dictionary where Key: StringProtocol {
func getInAmountFormat(str: String) -> String? {
if let value = self[str] as? Double {//Cannot subscript a value of type 'Dictionary<Key, Value>' with an index of type 'String'
return String(format: "%.2f", value)
}
return nil
}
}
【问题讨论】:
标签: swift dictionary