Firstly
let stringName: String? = (name as? NSString != nil) ? name as! String : nil
Can be simplified as
let stringName: String? = name as? String
But if we want to make the modification earlier, we can avoid the [String: Any]
from the start:
let floatValue = (data["value"] as? NSNumber)?.floatValue
let nameValue = data["name"] as? String
If data["value"]
doesn't exist, it will be nil. If it's not a NSNumber
(since it's the Objective-C basic way to encapsulate Float into a Object) it will be nil. In the end, the floatValue
will be nil or have the real value.
Same logic for the nameValue
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…