在Swift中为Firebase DataStructure创建类 [英] Creating class for Firebase DataStructure in Swift
问题描述
我是Firebase的新手,我创建了一个Firebase结构供我使用,基本上我想做的是列出所有用户FirebaseDB.
I am new to firebase, a Firebase structure was created for me to work on, what i want to do basically is to list all users the FirebaseDB.
我想快速创建一个对应的类到下面的firebase数据结构.
I want to create a corresponding class in swift to the firebase data structure below.
"users" : {
"A654tdC5uBPoKQzJnpZIpwOfsaJ3" : {
"groups" : {
"-KZ7fl3I4W7YWuGk6l9k" : true,
"-KclzmvvJF54cAg14P92" : true,
"-KclzpzrJOhCOWL5_jvw" : true,
"-Kcm33N1B_oiVYrQWh3n" : true,
"-Kcm3GfRaaGfztEBmflp" : true
},
"location" : {
"coordinates" : [ 6.59086, 3.3914267 ],
"name" : "Oyebanjo Solarin Street, Lagos",
"visibility" : true
},
"photoUrl" : "http://pbs.twimg.com/profile_images/511567946351923201/YBqqKc78_normal.jpeg",
"username" : "Kamiye",
"visibilityToSelectContacts" : {
"contacts" : {
"gFTCpzgSJDOsrVKWbAJp0Z1JFXp1" : true,
"rRxT6x87kgSjTwZfa7M8ZwzdFkC3" : true
},
"visibility" : true
}
},
这是我尝试过的并且无法正常工作
class LContact {
var visibliityToSelectedContacts: VisibilityToSelectContacts?
var photoUrl: String?
let username: String
var location: LALocation?
var groudId = [String]()
init (value: [String: Any] ) {
self.username = value["username"] as! String
self.photoUrl = value["photoUrl"] as? String
self.location = LALocation(value: value["coordinates"] as? [String:Any] ?? [:])
self.groudId = (value["groups"] as? [String])!
}
}
class VisibilityToSelectContacts {
var contacts = [String]()
var visibility: Bool
init(value: [String: Any]) {
self.contacts = [value["contacts"] as! String]
self.visibility = value["visibility"] as! Bool
}
}
struct LALocation {
var long: Double
var lat: Double
var address: String!
var visibility: Bool!
init(long: Double, lat: Double, address: String?, visibility: Bool) {
self.long = long
self.lat = lat
self.address = address
self.visibility = visibility
}
init?(value: [String: Any]) {
guard let long = value["0"] as? Double,
let lat = value["1"] as? Double else {return nil}
self.long = long
self.lat = lat
self.address = value["name"] as? String
self.visibility = value["visibility"] as? Bool
}
}
推荐答案
让我开始一些可能有用的代码.它不是整个课程,但所有概念都在那里.我还使用了简化的Firebase结构,但是概念仍然相同:
Let me get you going with some code that may help. It's not the entire class but all of the concepts are there. I also am using a simplified Firebase structure, but again, the concepts are the same:
这是Firebase结构
Here's the Firebase structure
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"
首先,每个用户似乎都有一个位置,这有助于使自己成为一个结构.
First there's appears to be a Location for each user thats lends itself to being a structure.
struct LocationStruct {
var coords: String?
var name: String?
var visibility: Bool?
}
然后我们在用户类中使用该结构.在此示例中,我们为单个用户传递快照以初始化类,并对其进行解构以填充类变量.
We then use that structure within our user class. In this example, we pass in a snapshot for a single user to initialize the class and deconstruct it to populate the class variables.
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
}
}
这是在单个用户中读取并填充UserClass的代码
Here's the code to read in a single user and populate a UserClass
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!)
})
结构中唯一剩下的问题是一个visibleToSelectContacts节点,该节点具有一个子节点联系人.
The only remaining issue in your structure, theres a visibilityToSelectContacts node, which has a child node contacts.
因此,从本质上讲,UserClass中的userDict高层词典将具有一个称为visibleToSelectContacts的子级,然后该子级具有一个具有以下子级词典的子联系人:"gFTCpzgSJDOsrVKWbAJp0Z1JFXp1"值对:true.像这样
So essentially the high level dictionary of userDict in the UserClass will have a child called visibilityToSelectContacts which then has a child contacts which has a child dictionary of key:value pairs of "gFTCpzgSJDOsrVKWbAJp0Z1JFXp1" : true. Like this
userDict
visibilityToSelectContacts
contacts
contant_0: true
contant_1: true
我将离开您,让该结构敲定(提示:这是相同的设计模式)
I will leave you to get that structure hammered out (hint: it's the same design pattern)
这篇关于在Swift中为Firebase DataStructure创建类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!