let json = [
"left" : 18,
"deadline" : "May 10",
"progress" : 0.6
] as [String: AnyObject]
let ss = json["progress"] as? Float
let sss = json["progress"] as? Double
print("ss = \(ss)\n sss = \(sss)")
I have no idea why the ss
shows nil while sss
shows 0.599999998
. Why does casting to Float
get nil? Do you guys have some methods so that I can get the correct result?
let json:[String: Any] = [ "left" : 18, "deadline" : "May 10", "progress" : 0.6 ]
– HerlFloat(0.6)
otherwise the compiler will infer the type (Double) – Herl(json["progress"] as? NSNumber)?.floatValue
– Herl"progress" : Float(0.6)
. Changing to this makes sense. Thanks for your help. – Lorinlet js:[String: Any] = [ "left" : 18, "deadline" : "May 10", "progress" : Float(0.6) ]
– Herl