I found odd behavior in the Swift standard library struct Double(). This function converts a string representing a number into a double precision number. All well and good.
I recently discovered odd behavior wrt to leading whitespace, namely the conversion fails if a single blank character space leads a valid number string:
Example (Xcode Debugger)
(lldb) po Double("11.8000")
▿ Optional<Double>
- some : 11.8
(lldb) po Double(" 11.8000")
nil
I have not seen this documented, so I post it here. The inelegant fix is:
let myNumber = Double(myString.trimmingCharacters(in: .whitespaces))
Any better solution? I mean aside from a simple String Extension to replace trimmingCharacters(in: .whitespaces) with trim(). I would think Double() and its relatives should be able to internally handle leading and trailing spaces.