Are there different solution how retrieve waypoints? - swift

"-KHbCuQflKHrJiUmfpG0" : {
"waypoints" : {
"-KHbCuQflKHrJiUmfpG1" : {
"latitude" : 13.17078652595298,
"longitude" : -59.5775944578738
},
"-KHbCuQflKHrJiUmfpG2" : {
"latitude" : 13.15541190861343,
"longitude" : -59.57619643155932
},
"-KHbCuQg9W_tebl1pU66" : {
"latitude" : 13.148444967591,
"longitude" : -59.5589266947333
}
},
"subtitle" : "jamesrick",
"title" : "Highway",
"type" : "polyline"
},
I have this structure for lines in Firebase. How retrieve all data with also nested node waypoints?
ref.observeEventType(.Value, withBlock: { polylines in
if let objects = polylines.children.allObjects as? [FDataSnapshot] {
for object in objects {
polylineDictionary = object.values as? Dictionary<String, AnyObjects> {
// ? ? ?
}
}
}
})
Now I have access to title, subtitle, type but how obtain access to waypoints? When I use
`polylineDictionary["waypoints"] as? [String: [String:Double]]`
so this dictionaries are not ordered. Thanks for some advice.

This is how I would get the nested waypoints... it's basically by iterating through the keys... I would also add some error checking when grabbing the longitude and latitudes to make sure they're there, but this is the gist:
if let polylineDictionary["waypoints"] as? [String: [String:Double]] {
let waypoints = Array(polylineDictionary.keys)
for i in 0..<waypoints.count {
let waypointId = waypoints[i]
let latitude = polylineDictionary[waypointId]["latitude"]
let longitutde = polylineDictionary[waypointId]["longitude"]
}
}

It's not clear from the question about ordering; if you just want to get to the waypoints, it's pretty straight forward:
Assume your complete Firebase structure is:
root_node
"-KHbCuQflKHrJiUmfpG0" : {
"waypoints" : {
"-KHbCuQflKHrJiUmfpG1" : {
"latitude" : 13.17078652595298,
"longitude" : -59.5775944578738
},
"-KHbCuQflKHrJiUmfpG2" : {
"latitude" : 13.15541190861343,
"longitude" : -59.57619643155932
},
"-KHbCuQg9W_tebl1pU66" : {
"latitude" : 13.148444967591,
"longitude" : -59.5589266947333
}
},
"subtitle" : "jamesrick",
"title" : "Highway",
"type" : "polyline"
}
Suppose I want the data for this specific node, -KHbCuQflKHrJiUmfpG0
let nodeRef = rootRef.childByAppendingPath("-KHbCuQflKHrJiUmfpG0")
nodeRef.observeSingleEventOfType(.Value , withBlock: { snapshot in
print(snapshot.value) //prints everything in the node
let title = snapshot.value["title"] as! String //to get any element
print(title) //prints Highway
var waypoints = [String: [String:String]]() //dict to hold key:value, unordered
waypoints = snapshot.value["waypoints"] as! Dictionary
print(waypoints) //prints the 3 waypoints and their children (as a dict)
//get fancy and convert the dictionary to an array (which can be sorted)
let arr = waypoints.map {"\($0) \($1)"}
for point in arr {
print(point)
}
}

Related

Swift - Decoding a Deeply Nested Dictionary

I am so close - but I am struggling with a very simple function to allow me to access a data point deeply nested in my JSON. The example I am using is on the Google directions API.
Sample JSON (from GMapsAPI):
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "ChIJ960bMolw44kRQcGOlOZQ-r8",
"types" : [ "premise" ]
},
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "EiMxMTggU2FsZW0gU3QsIEJvc3RvbiwgTUEgMDIxMTMsIFVTQSIaEhgKFAoSCSvDfDSJcOOJEbQanF0WxROfEHY",
"types" : [ "street_address" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 42.3647252,
"lng" : -71.0555085
},
"southwest" : {
"lat" : 42.3644965,
"lng" : -71.05552419999999
}
},
"copyrights" : "Map data ©2018 Google",
"legs" : [
{
"distance" : {
"text" : "82 ft",
"value" : 25
},
"duration" : {
"text" : "1 min",
"value" : 11
},
"end_address" : "118 Salem St, Boston, MA 02113, USA",
"end_location" : {
"lat" : 42.3647252,
"lng" : -71.0555085
},
"start_address" : "115 Salem St, Boston, MA 02113, USA",
"start_location" : {
"lat" : 42.3644965,
"lng" : -71.05552419999999
},
"steps" : [
{
"distance" : {
"text" : "82 ft",
"value" : 25
},
"duration" : {
"text" : "1 min",
"value" : 11
},
"end_location" : {
"lat" : 42.3647252,
"lng" : -71.0555085
},
"html_instructions" : "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eSalem St\u003c/b\u003e toward \u003cb\u003eJerusalem Pl\u003c/b\u003e",
"polyline" : {
"points" : "ciqaG~_upLO?]A"
},
"start_location" : {
"lat" : 42.3644965,
"lng" : -71.05552419999999
},
"travel_mode" : "DRIVING"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "ciqaG~_upLm#A"
},
"summary" : "Salem St",
"warnings" : [],
"waypoint_order" : []
}
],
"status" : "OK"
}
Decodable Structure: To work with this, I am using Decodable. I have been able to access first level nested data (routes.summary), but I am struggling to get further down (for example: routes.legs.duration). My code structure is as follows:
struct Directions: Decodable {
let status: String
let routes: [Routes]
enum CodingKeys :String, CodingKey {
case status, routes
}
struct Routes: Decodable {
let summary: String
let legs: [Legs]
enum CodingKeys : String, CodingKey {
case summary, legs
}
}
struct Legs: Decodable {
let duration: Duration
enum CodingKeys : String, CodingKey {
case duration
}
}
struct Duration: Decodable {
let text: String
enum CodingKeys : String, CodingKey {
case text
}
}
Implementation after URL set-up:
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else { return }
do {
let directions = try
JSONDecoder().decode(Directions.self, from: data)
for item in directions.routes {
self.stringoutput = item.summary
}
After all this, all I want to do is be able to access "text" in the JSON and return that value. The last line in the code is able to successfully return "summary" in the JSON; and I can print(directions) and the whole array/dictionary will return in the debug area, including "text". But I still can't figure out how to do:
x = directions.routes.legs.duration.text
to make x equal to "1 min"
Would be appreciative of anyone's help.
Edit: What ended up working is Vadian's struct keys below and the following for in loop:
for item in directions.routes {
print(item.summary)
self.direct = item.summary
for items in item.legs {
self.stringoutput = items.duration.text
print(items.duration.text)
}
Cheers!
These structs don't decode all keys, but it's a starting point.
If keys and struct members have the same name you don't need to specify CodingKeys
struct Directions: Decodable {
let status: String
let routes: [Route]
}
struct Route: Decodable {
let summary: String
let legs: [Leg]
}
struct Leg: Decodable {
let duration : TextValue
let distance : TextValue
let endAddress : String
let endLocation : Location
let startAddress : String
let startLocation : Location
let steps : [Step]
}
struct TextValue: Decodable {
let text: String
let value : Int
}
struct Location: Decodable {
let lat, lng : Double
}
struct Step: Decodable {
let duration : TextValue
let distance : TextValue
let endLocation : Location
let startLocation : Location
let htmlInstructions : String
let travelMode : String
}
To decode the snake_cased keys properly you have to add the appropriate key decoding strategy
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
To access the arrays ([]) you have to get an item by index
step[0]
or iterate the array with a loop
for step in steps {}

Get child nodes from firebase (Swift)

How can i get child nodes from firebase?
Firebase struct is here:
Me need to get a child "Photos" for each child above. And later add to collectionView each child "Photo".
My firebase struct:
{
"Студии" : {
"Бабулька" : {
"Photo" : {
"image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185"
},
"address" : "Сатурн",
"image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185",
"image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185",
"name" : "Карапулька"
},
"Дубаи" : {
"Photo" : {
"image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city1.jpg?alt=media&token=c442fd8f-4cc4-4e30-8242-975aaf5427dc"
},
"address" : "Тутушка",
"image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city1.jpg?alt=media&token=c442fd8f-4cc4-4e30-8242-975aaf5427dc",
"name" : "Дубаи"
},
"Лондон" : {
"Photo" : {
"image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/oko1.jpg?alt=media&token=fedea2cb-93b1-496c-9392-0b95f4ada7b5"
},
"address" : "Бейкер стрит",
"image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/oko1.jpg?alt=media&token=fedea2cb-93b1-496c-9392-0b95f4ada7b5",
"name" : "Лондон"
}
}
}
Thanks for help.
To get to the data you want, iterate over all of the child notes of the main parent node. Assume the following structure
posts
post_0
Photo
image: "https://www....."
post_1
Photo
image: "https://www....."
and the code to get the image urls...
let ref = self.ref.child("posts")
ref.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let snap = child as! DataSnapshot
let imageSnap = snap.childSnapshot(forPath: "Photo")
let dict = imageSnap.value as! [String: Any]
let url = dict["image"] as! String
print(url)
}
})

How retrieve coordinates in same order?

"-KHbCuQflKHrJiUmfpG0" : {
"coordinates" : {
"-KHbCuQflKHrJiUmfpG1" : {
"latitude" : 13.17078652595298,
"longitude" : -59.5775944578738
},
"-KHbCuQflKHrJiUmfpG2" : {
"latitude" : 13.15541190861343,
"longitude" : -59.57619643155932
},
"-KHbCuQg9W_tebl1pU66" : {
"latitude" : 13.148444967591,
"longitude" : -59.5589266947333
}
},
"subtitle" : "patrick",
"title" : "River",
"type" : "polyline"
},
This is my JSON structure for saving polylines and I need retrieve coordinates in same order.
This is my code for obtaining coordinates and creating polylines.
// geoobject.key - is ID of geoobject (for loop geoobjects) "-KHbCuQflKHrJiUmfpG0"
DataService.dataService.getGeoObject(geoobject.key).observeEventType(.Value, withBlock: { geoSnapshot in
var coordinatesPolyline = [CLLocationCoordinate2D]()
if let geoDictionary = geoSnapshot.value as? Dictionary<String, AnyObject> {
let id = geoSnapshot.key
if geoDictionary["type"] as! String == "polyline" {
if let coords = geoDictionary["coordinates"] as? [String:[String:Double]] {
let contents = coords.values
for content in contents {
let latitude = content["latitude"]
let longitude = content["longitude"]
let point = CLLocationCoordinate2D(latitude: latitude!, longitude: longitude!)
coordinatesPolyline.append(point)
}
// coordintesPolyline contains shuffle coordinates
let polyline = Polyline(coordinates: &coordinatesPolyline, count: coordinatesPolyline.count)
//coordinates = []
polyline.id = geoSnapshot.key
polyline.title = geoDictionary["title"] as? String
polyline.subtitle = geoDictionary["subtitle"] as? String
self.polylines.insert(polyline, atIndex: 0)
}
}
})
I rendered polylines but polylines are renderder bad because coordinates are shuffle.
Does anyone know how to get coordinates in the correct order. Firebase retrieve coordinates shuffle and then rendering doesn't work well.
Thank you for some advice.
When reading firebase data by .Value, ALL of the data within the node is returned in the snapshot.
If you want the data in a specific order, you need to decide what 'thing' you want it ordered by. The natural order is by key, but if you want it ordered by some other parameter, you need to include that in you firebase structure, and then add orderedBy to your observe.
One you have the data by .Value, you then need to iterate over each child within the snapshot to access that nodes data. For example
If your Firbase structure is
"coordinates" : {
"-KHbCuQflKHrJiUmfpG1" : {
"latitude" : 13.17078652595298,
"longitude" : -59.5775944578738
},
"-KHbCuQflKHrJiUmfpG2" : {
"latitude" : 13.15541190861343,
"longitude" : -59.57619643155932
},
"-KHbCuQg9W_tebl1pU66" : {
"latitude" : 13.148444967591,
"longitude" : -59.5589266947333
}
Then the snapshot contains All of that data. Iterate over the snapshot and work with each child:
ref.observeEventType(.Value, withBlock: { geoSnapshot in
for child in geoSnapshot.children {
let lat = child["latitude"] as? String
let long = child["longitude"] as? String
print("lat = \(lat) long = \(long)"
}
})
(didn't test that code but it's close)
Oh, and one other thing. ObserveEventOfType(.Value) will continually observe the node for changes - add, change or remove and will fire any time one of those events occur.
If you just want a one-shot, use ObserveSingleEventOfType(.Value)

Json results to a string array in swift

Hello i've been trying to make a json request and some of its results i want to put it to an array of string.
So i have the following code
var arrRes = [[String:AnyObject]]()
var nameaRR = [String]()
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, "https://graph.facebook.com/search", parameters: ["q": "", "type": "place", "center": "37.928319,23.7036673", "distance": "10000","limit": "1000", "access_token": "SomeToken", "expires_in": "5184000"])
.responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
//print(swiftyJsonVar)
if let resData = swiftyJsonVar["data"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
self.nameaRR = swiftyJsonVar["data"]["name"] as! [String]
print(self.nameaRR)
}
if self.arrRes.count > 0 {
self.kati.reloadData()
}
}
}
}
The JSON Resul is the following
{
"data" : [
{
"category_list" : [
{
"id" : "272705352802676",
"name" : "Outdoors"
},
{
"id" : "115725465228008",
"name" : "Region"
}
],
"id" : "552889064768971",
"name" : "Παλαιο Φαληρο", //This String i want to put in an Array
"category" : "Local business",
"location" : {
"street" : "",
"city" : "Palaión Fáliron",
"country" : "Greece",
"longitude" : 23.6944070162,
"zip" : "17562",
"latitude" : 37.9284637008,
"state" : ""
}
}
]
}
I get a warning
Cast from 'JSON' to unrelated type '[String]' always fails
But i'm stuck of how can i put all the Strngs to the array nameaRR.
Can anyone help me find my mistake? Thanks!
look do like that
if let resData = swiftyJsonVar["data"] as? [[String:AnyObject]] {
if let categorylist = resData["category_list"] as? [[String:AnyObject]]{
if let id = categorylist["id"] as? Int{
print(id)
}
}
}

how manipulate a NSDictionary generated by a json file in swift

I've a NSDictionary populated by a JSON file.
JSON file content (initially)
{
"length" : 0,
"locations" : []
}
I want add some elements in "locations". The elements have the below structure:
[
"name" : "some_name",
"lat" : "4.88889",
"long" : "5.456789",
"date" : "19/01/2015"
]
In next code I read de JSON File
let contentFile = NSData(contentsOfFile: pathToTheFile)
let jsonDict = NSJSONSerialization.JSONObjectWithData(contentFile!, options: nil, error: &writeError) as NSDictionary`
like you can see jsonDict contain the JSON's info but in a NSDictionary object.
At this point I can't add the elements mentioned before, I tried insert NSData, NSArray, Strings, and nothing results for me
After do this I want convert "final" NSDictionary in JSON again to save it in a file.
The "final" NSDictionary must be like this
{
"length" : 3,
"locations" : [
{
"name" : "some_name",
"lat" : "4.88889",
"long" : "5.456789",
"date" : "19/01/2015"
},
{
"name" : "some_name_2",
"lat" : "8.88889",
"long" : "9.456789",
"date" : "19/01/2015"
},
{
"name" : "some_name_3",
"lat" : "67.88889",
"long" : "5.456789",
"date" : "19/01/2015"
}
]
}
"length" control the index for new element
I have no more ideas to do this. thanks in advance
If you want to be able to modify the dictionary, you can make it mutable:
let jsonDict = NSJSONSerialization.JSONObjectWithData(contentFile!, options: .MutableContainers, error: &writeError) as NSMutableDictionary
The resulting NSMutableDictionary can be modified. For example:
let originalJSON = "{\"length\" : 0,\"locations\" : []}"
let data = originalJSON.dataUsingEncoding(NSUTF8StringEncoding)
var parseError: NSError?
let locationDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers, error: &parseError) as NSMutableDictionary
locationDictionary["length"] = 1 // change the `length` value
let location1 = [ // create dictionary that we'll insert
"name" : "some_name",
"lat" : "4.88889",
"long" : "5.456789",
"date" : "19/01/2015"
]
if let locations = locationDictionary["locations"] as? NSMutableArray {
locations.addObject(location1) // add the location to the array of locations
}
If you now constructed JSON from the updated locationDictionary, it would look like:
{
"length" : 1,
"locations" : [
{
"long" : "5.456789",
"lat" : "4.88889",
"date" : "19/01/2015",
"name" : "some_name"
}
]
}