提问人:Lalit Waliyan 提问时间:10/17/2017 最后编辑:Lalit Waliyan 更新时间:10/17/2017 访问量:110
字符串值没有可变行为 (swift 3)
String value is not have mutable behaviour (swift 3)
问:
我将我的 String 值转换为 Double 显示在下面的代码中。在最后一个变量中,我得到字符串值,即 ex - 1435.76 但是当我转换为 Double 值时显示 nill。请帮我摆脱这个.提前致谢
self.cart = result.value(forKey: "cart") as! NSMutableArray
self.totalsDic = result.value(forKey: "totals") as! NSMutableDictionary
print(self.totalsDic)
// print(self.cart)
let total = self.totalsDic.object(forKey: "total") as! NSMutableDictionary
let final = total.object(forKey: "total") as? String
// final = final?.replacingOccurrences(of: "Rs.", with: "")
let myDouble1 = NumberFormatter().number(from: final!)?.doubleValue
print("My double: \(String(describing: myDouble))")
let myDouble2 = Double(final!)
答: 暂无答案
评论
Double(final!)
仅当 final 为 nil 或包含无法转换为 Double 的字符串时,才返回 nil。因此,您最好的选择是使用它,这将确保如果它无法将字符串转换为 Double 将下降到安全值 0。最后,而不是使用 !(强制展开)使用或根据您的需要.doubleValue
if let
guard let