gpt4 book ai didi

ios - 为从 firestore 加载数据创建结构或类

转载 作者:行者123 更新时间:2023-11-28 15:08:00 25 4
gpt4 key购买 nike

我需要创建一个Struct 以从Firestore 加载数据。但我不想使用数组,我只想使用字符串每次加载相同的数据。

现在我正在使用这段代码:

var studioHallName: String = ""
var startTimeWeekdays: String = ""
var endTimeWeekdays: String = ""

override func viewDidLoad() {
super.viewDidLoad()

Firestore.firestore().collection("firestore").whereField("id", isEqualTo: hallID).getDocuments() {
querySnapshot, error in

if let error = error {

print("\(error.localizedDescription)")

} else {

for document in (querySnapshot?.documents)! {

if let HallName = document.data()["studioHallName"] as? String {
self.studioHallName = HallName
}
if let hallStartTimeWeekdays = document.data()["startTimeWeekdays"] as? String {
self.startTimeWeekdays = hallStartTimeWeekdays
}
if let hallEndTimeWeekdays = document.data()["endTimeWeekdays"] as? String {
self.endTimeWeekdays = hallEndTimeWeekdays
}
}

DispatchQueue.main.async {

self.tableView.reloadData()

}

}

}

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 1

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "newDetailTableCell", for: indexPath) as! NewDetailTableViewCell

cell.nameLabel.text = studioHallName
cell.descriptionLabel.text = startTimeWeekdays
cell.addressLabel.text = endTimeWeekdays

return cell

}

如何创建一个StructClass 以便以后重用它?我如何使用此 StructClassFirestore 加载数据?

最佳答案

你有很多选择,但这是非常基础的东西,所以你绝对应该阅读类和结构。

这个结构的基本实现看起来像这样:

struct StudioHallSession { //Just guessing on the name.  I don't have context.

let studioHallName: String
let startTime: String
let endTime: String
}

这就是基本实现所需的全部内容。除非您需要自定义逻辑,否则将推断出基本的 init

至于使获取可重用,一个非常基本的实现如下所示:

static func getSession(forHallId hallId: String, _ completion: (_ session: StudioHallSession?)->()) {
Firestore.firestore().collection("firestore").whereField("id", isEqualTo: hallID).getDocuments() {
querySnapshot, error in
if let error = error {
print("\(error.localizedDescription)")
} else {
for document in (querySnapshot?.documents)! {
if let HallName = document.data()["studioHallName"] as? String {
self.studioHallName = HallName
}
if let hallStartTimeWeekdays = document.data()["startTimeWeekdays"] as? String {
self.startTimeWeekdays = hallStartTimeWeekdays
}
if let hallEndTimeWeekdays = document.data()["endTimeWeekdays"] as? String {
self.endTimeWeekdays = hallEndTimeWeekdays
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}

为了使用这个函数,你需要从你的 viewDidLoad 中删除那个函数,然后用这个替换它:

var studioHallSession: StudioHallSession?

override func viewDidLoad() {
super.viewDidLoad()
StudioHallSession.getSession(forHallId: hallId) { (session) in
//Check to see if you got a valid session, and assign to your session variable
if let session = session {
self.studioHallSession = session
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}

最后,您必须修改 tableView 方法来处理从此 session 对象获取信息,如下所示:

cell.nameLabel.text = studioHallSession?.studioHallName
cell.descriptionLabel.text = studioHallSession?.startTime
cell.addressLabel.text = studioHallSession?.endTime

当然,您必须处理在 Firestore 等中找不到对象的情况。

关于ios - 为从 firestore 加载数据创建结构或类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48155319/

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