我需要创建结构以从 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
}
如何创建结构或类以便以后重复使用?我如何使用 Struct 或 Class 从 Firestore 加载数据?
答案 0 :(得分:1)
你有很多选择,但这是非常基本的东西,所以你一定要阅读Classes和Structs。
这个Struct的基本实现看起来像这样:
struct StudioHallSession { //Just guessing on the name. I don't have context.
let studioHallName: String
let startTime: String
let endTime: String
}
这就是基本实现所需的全部内容。除非您需要自定义逻辑,否则将推断出基本的init
。
至于使fetch可重用,一个非常基本的实现看起来像这样:
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方法以处理从此会话对象获取信息,如下所示:
cell.nameLabel.text = studioHallSession?.studioHallName
cell.descriptionLabel.text = studioHallSession?.startTime
cell.addressLabel.text = studioHallSession?.endTime
当然,您必须处理在Firestore中找不到对象的情况等。