xcode - 从 firebase 调用子数据
问题描述
晚上好,
我正在尝试调用数据以发布到与 tinder 非常相似的卡上。当我运行我的代码时,一切正常,我在控制台中看到了打印语句。但是,卡片视图显示带有默认文本的默认图像。我想知道是否有人遇到过这个问题,并且可以帮助解释我做错了什么。
fileprivate func fetchUsersFromDatabase() {
Database.database().reference().child("JobPost").observeSingleEvent(of: .value, with: {(Snapshot) in
if let eachDict = Snapshot.value as? NSDictionary{
for each in eachDict{
//I think the issue is caused by the let post = poster
let post = Poster(dictionary: Snapshot.value as! [String : Any])
self.cardViewModels.append(post.toCardViewModel())
print(each.value )
}
}
self.setupDummyCards()
}, withCancel: {(Err) in
})
}
// 结构在扩展文件中。
struct Poster: ProducesCardViewModel{
var jobName : String?
var price: Int?
var postName: String?
var ImageUrl1: String?
var uid: String?
init(dictionary: [String: Any]) {
self.price = dictionary["cost"] as? Int
self.jobName = dictionary["category"] as? String
self.postName = dictionary["description"] as? String ?? ""
self.ImageUrl1 = dictionary["JobImageUrl"] as? String ?? ""
self.uid = dictionary["fromId"] as? String ?? ""
}
func toCardViewModel() -> CardViewModel {
let attributedText = NSMutableAttributedString(string: jobName ?? "", attributes: [.font: UIFont.systemFont(ofSize: 32, weight: .heavy)])
let priceString = price != nil ? "\(price!)" : "N\\A"
attributedText.append(NSAttributedString(string: " \(priceString)", attributes: [.font: UIFont.systemFont(ofSize: 24, weight: .regular)]))
let jobString = jobName != nil ? jobName! : "Not available"
attributedText.append(NSAttributedString(string: "\n\(jobString)", attributes: [.font: UIFont.systemFont(ofSize: 20, weight: .regular)]))
return CardViewModel(imageNames: [ImageUrl1 ?? "" ], attributedString: attributedText, textAlignment: .left)
}
}
// toCardViewModel
import UIKit
protocol ProducesCardViewModel {
func toCardViewModel() -> CardViewModel
}
class CardViewModel {
let JobimageName: [String]
let attributedString: NSAttributedString
let textAlignment: NSTextAlignment
init(imageNames: [String], attributedString: NSAttributedString, textAlignment: NSTextAlignment) {
self.JobimageName = imageNames
self.attributedString = attributedString
self.textAlignment = textAlignment
}
fileprivate var imageIndex = 0 {
didSet {
let imageName = JobimageName[imageIndex]
let image = UIImage(named: imageName)
imageIndexObserver?(imageIndex, image)
}
}
var imageIndexObserver: ((Int, UIImage?) -> ())?
func advanceToNextPhoto() {
imageIndex = min(imageIndex + 1, JobimageName.count - 1)
}
func goToPreviousPhoto() {
imageIndex = max(0, imageIndex - 1)
}
}
先感谢您!
// PS 我之前发布了这个问题,但没有足够的细节。我决定用优质材料重新发布它。我真的很感谢你的时间。
解决方案
我想出了一个答案,这对我来说很明显。我注意到
print(each.value )
会打印值,所以我只是替换
let post = Poster(dictionary: Snapshot.value as! [String : Any])
至
let post = Poster(dictionary: each.value as! [String : Any])
一切都开始正常了!
推荐阅读
- c++ - 将 R2R FFT 从 FFTW 移植到 cuFFT
- javascript - 如何在快递中将 TINYINT(1) 转换为 BOOLEAN?
- python - TypeError: name() 参数 1 必须是 unicode 字符,而不是 str python
- weblogic - 无法从找不到 Weblogic.Server 错误中启动 Weblogic
- vb.net - 组合框中的鼠标滚动事件
- python - Python Open CV & yolo 对象检测和跟踪
- python-3.x - Django Q Tasks 在从管理页面单击排队任务时出错
- python - 如何使用 Beautifulsoup 从网页中查找特定的脚本标签
- flutter - 如何将切换按钮放在右侧
- django - 使用不同端口在 localhost 上进行跨项目 iframe 测试