Created
June 6, 2014 08:07
-
-
Save rhult/5f26d69939214d5f2a3f to your computer and use it in GitHub Desktop.
Naive String.toDouble() in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func toDouble() -> Double? { | |
var formatter = NSNumberFormatter() | |
let number = formatter.numberFromString(self) | |
if number == nil { | |
return nil | |
} | |
return number.doubleValue | |
} | |
} | |
if let d = "123.1".toDouble() { | |
println(d) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't compile in Swift 1.0. It just needs a minor change though: