我有这段代码,我想从FirebaseDatase加载一些数据
func fetchVinylsData(){
SVProgressHUD.show()
guard let currentUID = Auth.auth().currentUser?.uid else { return }
dbRef.child("vinyls").child(currentUID).observe(.childAdded) { (snapshot) in
guard let dictionary = snapshot.value as? Dictionary<String,AnyObject> else { return }
let vinyl = Vinyl(dictionary: dictionary)
print(dictionary)
self.vinyls = [Vinyl]()
self.vinyls.append(vinyl)
self.vinyls.sort(by: { (vinyl1, vinyl2) -> Bool in
return vinyl1.artist < vinyl2.artist
})
self.tableView.reloadData()
}
SVProgressHUD.dismiss()
}当我打印字典时,我可以很好地获得所有记录,但是我的表视图不会被填充。我一直工作得很好,直到我决定向Vinyl.swift中添加一个新字段。firebase数据库的结构没有新的field.is这是个问题吗?以下是Vinyl.swift的代码:
import UIKit
import Firebase
class Vinyl {
var vinylID : String!
var title : String!
var artist : String!
var label : String!
var vinylCountry : String!
var isOut : Bool = false
var vinylLocation : String!
var year : String!
var vinylAutoID : String!
var vinylFormat : String!
init(dictionary : Dictionary<String,AnyObject>) {
guard let title = dictionary["title"] as? String else { return }
guard let artist = dictionary["artist"] as? String else { return }
guard let label = dictionary["label"] as? String else { return }
guard let vinylID = dictionary["vinylID"] as? String else { return }
guard let vinylCountry = dictionary["vinylCountry"] as? String else { return }
guard let vinylLocation = dictionary["vinylLocation"] as? String else { return }
guard let year = dictionary["vinylYear"] as? String else { return }
guard let autoID = dictionary["autoID"] as? String else { return }
guard let isOut = dictionary["isOut"] as? Bool else { return }
guard let vinylFormat = dictionary["format"] as? String else { return }
self.vinylID = vinylID
self.title = title
self.artist = artist
self.label = label
self.vinylCountry = vinylCountry
self.isOut = isOut
self.vinylLocation = vinylLocation
self.year = year
self.vinylAutoID = autoID
self.vinylFormat = vinylFormat
}
}发布于 2019-04-07 19:03:44
我不建议在init方法中使用guard let,因为假设dictionary["title"]为空或不可用,那么它将从代码的第一行返回,其他代码也会发生同样的情况。要解决这个问题,您可以使用Nil-Coalescing Operator (这意味着您可以指定默认值nil或任何其他值,如果something["something"]为nil或optional)
替换
guard let title = dictionary["title"] as? String else { return }
guard let artist = dictionary["artist"] as? String else { return }
guard let label = dictionary["label"] as? String else { return }
guard let vinylID = dictionary["vinylID"] as? String else { return }
guard let vinylCountry = dictionary["vinylCountry"] as? String else { return }
guard let vinylLocation = dictionary["vinylLocation"] as? String else { return }
guard let year = dictionary["vinylYear"] as? String else { return }
guard let autoID = dictionary["autoID"] as? String else { return }
guard let isOut = dictionary["isOut"] as? Bool else { return }
guard let vinylFormat = dictionary["format"] as? String else { return }使用
let title = dictionary["title"] as? String ?? ""
let artist = dictionary["artist"] as? String ?? ""
let label = dictionary["label"] as? String ?? ""
let vinylID = dictionary["vinylID"] as? String ?? ""
let vinylCountry = dictionary["vinylCountry"] as? String ?? ""
let vinylLocation = dictionary["vinylLocation"] as? String ?? ""
let year = dictionary["vinylYear"] as? String ?? ""
let autoID = dictionary["autoID"] as? String ?? ""
let isOut = dictionary["isOut"] as? Bool ?? false
let vinylFormat = dictionary["format"] as? String ?? ""在处理async调用时,您还需要更新main queue中的UI,如下所示:
DispatchQueue.main.async {
self.tableView.reloadData()
}https://stackoverflow.com/questions/55553129
复制相似问题