📅  最后修改于: 2022-03-11 15:01:00.259000             🧑  作者: Mango
//Firebase RealtimeDB Class
_ users
|
|_ a_uid
|
|_ email: "some email"
|
|_ groups
| |_ group_1: true
| |_ group_2: true
|
|_ location
| |_ coords: "[52.5, 67.1]"
| |_ name: "Oyebanjo Solarin Street, Lagos"
| |_ visibility: true
|
|_ username: "some username"
//1st
struct LocationStruct {
var coords: String?
var name: String?
var visibility: Bool?
}
//2nd
class UserClass {
var email = ""
var username = ""
var groupsDict: [String: Any]
var loc = LocationStruct()
init(snap: FIRDataSnapshot) {
let userDict = snap.value as! [String: Any]
self.email = userDict["email"] as! String
self.username = userDict["username"] as! String
self.groupsDict = userDict["groups"] as! [String: Any]
let locationDict = userDict["location"] as! [String: Any]
self.loc.coords = locationDict["coords"] as? String
self.loc.name = locationDict["name"] as? String
self.loc.visibility = locationDict["visibility"] as? Bool
}
}
//3rd
ref.child("users").child("a_uid")
.observeSingleEvent(of: .value, with: { snapshot in
let user = UserClass(snap: snapshot)
//this code is just to show the UserClass was populated.
print(user.email)
print(user.username)
for group in user.groupsDict { //iterate over groups
print(group) //and print each one
}
print(user.loc.coords!) //print the location data
print(user.loc.name!)
print(user.loc.visibility!)
})