To convert a string to double, we can use the built-in
Double() initializer in Swift.
Double()initializer takes the string as an input and returns the double instance. If the string contains invalid characters or invalid format it returns nil.
Here is an example, that converts the string
13.344 to Double:
let string = "13.344" let myDouble = Double(string) ?? 0 print(myDouble)
Similarly, we can also use the following syntax to do it.
import Foundation let string = "13.344" let myDouble = (string as NSString).doubleValue print(myDouble)