首页 > 解决方案 > 从 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 我之前发布了这个问题,但没有足够的细节。我决定用优质材料重新发布它。我真的很感谢你的时间。

标签: xcodefirebasefirebase-realtime-database

解决方案


我想出了一个答案,这对我来说很明显。我注意到

print(each.value ) 

会打印值,所以我只是替换

let post = Poster(dictionary: Snapshot.value as! [String : Any])

 let post = Poster(dictionary: each.value as! [String : Any])

一切都开始正常了!


推荐阅读