gpt4 book ai didi

ios - 在 Swift 中为 Firebase 数据结构创建类

转载 作者:搜寻专家 更新时间:2023-10-31 08:30:49 24 4
gpt4 key购买 nike

我是 firebase 的新手,为我创建了一个 Firebase 结构,我想做的基本上是列出 FirebaseDB 的所有用户。

我想在 swift 中为下面的 firebase 数据结构创建一个对应的类。

 "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
}
}

最佳答案

让我为您介绍一些可能有帮助的代码。这不是整个类(class),但所有的概念都在那里。我也使用简化的 Firebase 结构,但同样,概念是相同的:

这是 Firebase 结构

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"

首先,似乎每个用户都有一个位置,这有助于成为一个结构。

   struct LocationStruct {
var coords: String?
var name: String?
var visibility: Bool?
}

然后我们在我们的用户类中使用该结构。在此示例中,我们传入单个用户的快照以初始化类并解构它以填充类变量。

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 的代码

    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!)
})

结构中唯一剩下的问题是 visibilityToSelectContacts 节点,它有一个子节点联系人。

所以本质上,UserClass 中 userDict 的高级字典将有一个名为 visibilityToSelectContacts 的子字典,然后有一个子联系人,它有一个键值对子字典:“gFTCpzgSJDOsrVKWbAJp0Z1JFXp1”:true。像这样

userDict
visibilityToSelectContacts
contacts
contant_0: true
contant_1: true

我会让你敲定那个结构(提示:这是相同的设计模式)

关于ios - 在 Swift 中为 Firebase 数据结构创建类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42261620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com