How to pass a variable from string to double in Swift 2.

1

Hello I need to pass a String to Double , in this case the data will be passed from one ViewController to another via prepareForSegue and the < strong> Double I will use it to center a map in the position that you gave it by variable.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "Mapa_Rucapillan" {
        var nextScene = segue.destinationViewController as! mapController
            nextScene.ResivedLat = "100.0"
            nextScene.ResivedLong = "40.0"
    }

Here I send the data.

var ResivedLat: String = ""
var ResivedLong: String = ""

there they are received and I want to pass them to Double.

    
asked by Nicolas Rudisky 29.08.2016 в 21:56
source

2 answers

1

It's very simple

let string = "3.0"
let double = Double(string)

This method returns a Optional so if the conversion is unsuccessful, it will return nil

    
answered by 29.08.2016 / 23:15
source
1

In Swift 2 you can use Double () :

Double("3.141592")

If the value is not numeric,

Double("no_es_numerico")

You would get nil

    
answered by 30.08.2016 в 03:58