In Swift, how do I convert number strings in an array to Doubles -
i have large json array of nations , coordinates. sample element looks 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 have loop supposed create annotations data in 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 won't work unless can strip quotation marks latitude , longitudes. tried doing this:
location["capitallatitude"] = double(location[capitallatitude]) location["capitallongitude"] = double(location[capitallongitude])
but gives me error on account of trying connect unrelated type values.
the error value nil after unwrapping optional. understand i'm not providing lat/long numbers correctly.
how can rid of quotation marks around lat/long numbers? there far many manually.
you trying access location
type:
location["capitallatitude"] = double(location["capitallatitude"]) location["capitallongitude"] = double(location["capitallongitude"])
try that.
Comments
Post a Comment