简体   繁体   中英

In Swift, how do I convert number strings in an array to Doubles

I have a large JSON array of nations and coordinates. A sample element looks like this:

["CountryName":"El Salvador","CapitalName":"SanSalvador","CapitalLatitude":"13.7","CapitalLongitude":"-89.200000","CountryCode":"SV","ContinentName":"Central America"],["CountryName":"Equatorial Guinea","CapitalName":"Malabo","CapitalLatitude":"3.75","CapitalLongitude":"8.783333","CountryCode":"GQ","ContinentName":"Africa"]

Then I have a for loop that is supposed to create annotations from the data in the array.

for location in locations {
    let annotation = MGLPointAnnotation()
    annotation.title = location["CountryName"] as? String
    annotation.coordinate = CLLocationCoordinate2D(latitude: location["CapitalLatitude"] as! Double, longitude: location["CapitalLongitude"] as! Double)
    mapView.addAnnotation(annotation)
}

But this won't work unless I can strip the quotation marks from the latitude and longitudes. I tried doing this:

location["CapitalLatitude"] = Double(location[CapitalLatitude])
location["CapitalLongitude"] = Double(location[CapitalLongitude])

but that gives me an error on account of trying to connect unrelated type values.

The error is that a value is nil after unwrapping an optional. I understand that I'm not providing the lat/long numbers correctly.

How can I get rid of the quotation marks around the lat/long numbers? There are far too many to do it manually.

You are trying to access location with a type:

location["CapitalLatitude"] = Double(location["CapitalLatitude"])
location["CapitalLongitude"] = Double(location["CapitalLongitude"])

Try that.

The problem is that your data storage is stupid. Convert it to something you actually want, like a struct:

struct Country {
    let name : String
    let capitalName : String
    let capitalLatitude : Double
    let capitalLongitude : Double
    let code : String
    let continent : String
}

The conversion is just one line of code:

let arr = [["CountryName":"El Salvador","CapitalName":"SanSalvador","CapitalLatitude":"13.7","CapitalLongitude":"-89.200000","CountryCode":"SV","ContinentName":"Central America"],["CountryName":"Equatorial Guinea","CapitalName":"Malabo","CapitalLatitude":"3.75","CapitalLongitude":"8.783333","CountryCode":"GQ","ContinentName":"Africa"]]
let arr2 = arr.map {
    Country(name: $0["CountryName"]!,
            capitalName: $0["CapitalName"]!,
            capitalLatitude: Double($0["CapitalLatitude"]!)!,
            capitalLongitude: Double($0["CapitalLongitude"]!)!,
            code: $0["CountryCode"]!,
            continent: $0["ContinentName"]!
    )
}


And bingo, you've got an array of Country and now working with it is convenient and easy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM