gpt4 book ai didi

swift - 如何创建词典扩展

转载 作者:行者123 更新时间:2023-11-28 13:35:56 25 4
gpt4 key购买 nike

<分区>

我有这段代码可以从 [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
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com