Skip to content

Instantly share code, notes, and snippets.

@fongd
Last active July 27, 2016 20:07
Show Gist options
  • Save fongd/6f517fdcc7a28373e30d4930c02afe49 to your computer and use it in GitHub Desktop.
Save fongd/6f517fdcc7a28373e30d4930c02afe49 to your computer and use it in GitHub Desktop.
//
// Not sure how to return "data" from getTrailWarnings...
//
func getTrailWarnings() -> Array<Dictionary<String, AnyObject>>? {
let serviceUrl = "http://www.example.com/json"
guard let url = NSURL(string: serviceUrl) else { return nil }
requestTrailWarnings(url) { (data: Array<Dictionary<String, AnyObject>>?) in
return data
}
}
private func requestTrailWarnings(url: NSURL, completion: (Array<Dictionary<String, AnyObject>>?) -> Void) -> Void {
let request = NSMutableURLRequest(URL: url)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: {
(data, response, error) in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
if error == nil {
if let data = data {
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves) as? NSDictionary {
guard let features = json["features"] as? NSArray else { return }
guard features.count > 0 else { return }
var warnings = [Dictionary<String, AnyObject>]()
for feature in features {
if let feature = feature as? NSDictionary,
let attributes = feature["attributes"] as? NSDictionary,
let geometry = feature["geometry"] as? NSDictionary {
if let trailName = attributes["Name_Trail"] as? NSString where geometry.count == 2 {
let warning: Dictionary = [
"trailName": trailName,
"geometry": geometry
]
warnings.append(warning)
}
}
}
// return warnings to caller
completion(warnings)
}
} catch {
}
}
}
})
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
task.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment