This is how I approached it. I did not want to "cross the bridge", as it has been removed from Xcode 6 beta 5 anyway, quick and dirty:
extension String {
// converting a string to double
func toDouble() -> Double? {
// split the string into components
var comps = self.componentsSeparatedByString(".")
// we have nothing
if comps.count == 0 {
return nil
}
// if there is more than one decimal
else if comps.count > 2 {
return nil
}
else if comps[0] == "" || comps[1] == "" {
return nil
}
// grab the whole portion
var whole = 0.0
// ensure we have a number for the whole
if let w = comps[0].toInt() {
whole = Double(w)
}
else {
return nil
}
// we only got the whole
if comps.count == 1 {
return whole
}
// grab the fractional
var fractional = 0.0
// ensure we have a number for the fractional
if let f = comps[1].toInt() {
// use number of digits to get the power
var toThePower = Double(countElements(comps[1]))
// compute the fractional portion
fractional = Double(f) / pow(10.0, toThePower)
}
else {
return nil
}
// return the result
return whole + fractional
}
// converting a string to float
func toFloat() -> Float? {
if let val = self.toDouble() {
return Float(val)
}
else {
return nil
}
}
}
// test it out
var str = "78.001"
if let val = str.toFloat() {
println("Str in float: \(val)")
}
else {
println("Unable to convert Str to float")
}
// now in double
if let val = str.toDouble() {
println("Str in double: \(val)")
}
else {
println("Unable to convert Str to double")
}