What I would do is the following:
First I would create a struct
for People, to save a model
for every Person
entry.
You create a new Swift file and enter the following:
struct People {
var name: String = ""
var age: String = ""
var latitude: String = ""
var longitude: String = ""
var nationality: String = ""
}
Then, in your ViewController
class, you create an NSArray
of People
and instantiate is as empty.
var peoples: [People] = []
Then you create a function to download your desired data.
func loadPeople() {
// first you need to get into your desired .child, what is in your case People
let usersRef = firebase.child("People")
usersRef.observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists() {
// since we're using an observer, to handle the case
// that during runtime people might get appended to
// the firebase, we need to removeAll, so we don't
// store people multiple times
self.peoples.removeAll()
// then we sort our array by Name
let sorted = (snapshot.value!.allValues as NSArray).sortedArrayUsingDescriptors([NSSortDescriptor(key: "Name",ascending: false)])
for element in sorted {
let name = element.valueForKey("Name")! as? String
let age = element.valueForKey("age")! as? String
let location = element.valueForKey("location")! as? NSDictionary
let nationality = element.valueForKey("nationality")! as? String
// then we need to get our lat/long out of our location dict
let latitude = location.valueForKey("latitude")! as? String
let longitude = location.valueForKey("longitude")! as? String
// then we create a model of People
let p = People(name: name!, age: age!, latitude: latitude!, longitude: longitude!, nationality: nationality!)
// then we append it to our Array
self.tweets.append(t)
}
}
// if we want to populate a table view, we reload it here
// self.tableView.reloadData()
})
}
We need to call the function in viewDidAppear, after the UITableView
for example, is loaded.
override viewDidAppear() {
loadPeople()
}
Now we have an Array of People and are able to populate a UITableView or print the values:
for p in peoples {
print("name = \(p.name)")
print("longitude = \(p.longitude)")
print("latitude = \(p.longitude)")
}