I am looking for the best way to make this code in one line:
if (dictionary["Amoumt"] is Double) {
amount = dictionary["Amount"] as Double
} else {
amount = NSString(string: dictionary["Amount"] as String).doubleValue
}
I have Dictionary<String, AnyObject>, and I would like to analyze the values from it. I am using code as shown above, but too many lines. I would like to do this on one line. Sort of:
dictionary["Amount"].parseDouble()
There is no need to create an extension using this method:
func parseDouble() -> Double {
if (self is Double) {
return self as Double
} else {
return NSString(string:(self as String)).doubleValue
}
}
But what type should I extend? Could you help me using the general method? Therefore, I could call something like this:
dictionary["Amount"].parse(Double)
And this is a good way how to do it or should I do it differently?
source
share