Skip to content

Instantly share code, notes, and snippets.

@DonMag
Created May 13, 2016 17:39
Show Gist options
  • Save DonMag/aa961041ac88a304336fe9edf4c006fa to your computer and use it in GitHub Desktop.
Save DonMag/aa961041ac88a304336fe9edf4c006fa to your computer and use it in GitHub Desktop.
do {
let jsonDict = try NSJSONSerialization.JSONObjectWithData(fileContents!, options: []) as! NSDictionary
if let features = jsonDict["features"] as? [Dictionary<String, AnyObject>] {
// try this
var oneFeature = features[0]["geometry"]!["coordinates"]!![0][3]
for feature in features {
if let geometry = feature["geometry"] as? Dictionary<String, AnyObject> {
if let coordinates = geometry["coordinates"] as? Dictionary<Int, AnyObject> {
let coordinatesPair = coordinates[0]
// let path = GMSMutablePath()
// for coords in coordinatesPair {
// let longitude = coords[0].doubleValue
// let latitude = coords[1].doubleValue
// // print("Latitude: \(latitude), Longitude: \(longitude)")
//
// path.addCoordinate(CLLocationCoordinate2D(latitude: latitude, longitude: longitude))
// }
//
// let polyline = GMSPolyline(path: path)
// polyline.strokeColor = UIColor.redColor()
// polyline.strokeWidth = 5
// polyline.map = mapView
}
}
}
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
} catch {
}
@erica
Copy link

erica commented May 13, 2016

do {
    guard let fileContents = fileContents else { return }
    guard let jsonDict = try NSJSONSerialization.JSONObjectWithData(fileContents, options: []) as? NSDictionary else { return }
    guard let featuresArray = jsonDict["features"] as? NSArray else { return }
    guard featuresArray.count > 0 else { return }
    guard let featuresDict = featuresArray[0] as? NSDictionary else { return }
    guard let coordinatesArray = featuresDict["geometry"] else { return }
    guard coordinatesArray.count > 0 else { return }
    guard let coordinateArray = coordinatesArray[0] as? NSArray else { return }
    guard coordinateArray.count > 3 else { return }
    var oneFeature = coordinateArray[3]
    ...stuff...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment