FirebaseUI swift 3 - 如何在结构中获取FUIIndexTableViewDataSource快照

时间:2017-02-16 20:49:25

标签: firebase swift3 firebase-realtime-database firebaseui

我正在尝试使用以下FirbaseUI函数将对象放入结构中:

let indexQuery = ref.child("placePromotions").child(thisPlace.key)

let dataRef = ref.child("promotions")

dataSource = tableView.bind(toIndexedQuery: indexQuery, data: dataRef, delegate: self, populateCell: { (tableView, indexPath, snapshot) -> UITableViewCell in

let cell:PromotionsCell = tableView.dequeueReusableCell(withIdentifier: "promotionsCell", for: indexPath) as! PromotionsCell

if let snapshot = snapshot {
        print(snapshot)
    }
return cell
    })

当我打印快照时,我得到以下结果:

Snapshot:  Snap (active) 1
Snapshot:  Snap (archived) 0
Snapshot:  Snap (end_date) 1487967180000
Snapshot:  Snap (logo_cache) 1487276008635
Snapshot:  Snap (long_description) lange beschrijving
Snapshot:  Snap (place_id) -Kcrn0--Lno08fZ-8l-O
Snapshot:  Snap (short_description) hallo wereld!!!
Snapshot:  Snap (stamp_price) 10
Snapshot:  Snap (stamps) 1
Snapshot:  Snap (start_date) 1487621580000
Snapshot:  Snap (title) Stan's sneeuwschep-actie
Snapshot:  Snap (-Kd7YgueVAZaXZa30qyW) {
    active = 1;
    archived = 0;
    "end_date" = 1487967180000;
    "logo_cache" = 1487276008635;
    "long_description" = "lange beschrijving";
    "place_id" = "-Kcrn0--Lno08fZ-8l-O";
    "short_description" = "hallo wereld!!!";
    "stamp_price" = 10;
    stamps = 1;
    "start_date" = 1487621580000;
    title = "Stan's sneeuwschep-actie";
}

我制作了一个PromotionDataModelFirebase结构来匹配该位置的数据。 这不起作用:

let promotion = PromotionDataModelFirebase(snapshot: snapshot) 

如何将其添加到结构中?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

通过添加??解决它? [:]到struct

中的init
init(snapshot: FIRDataSnapshot) {
    let value = snapshot.value as? [String:AnyObject] ?? [:]
    active = value["active"] as? Bool ?? true
    archived = value["archived"] as? Bool ?? true
    end_date = value["end_date"] as? Int ?? 0
    logo_cache = value["logo_cache"] as? Int ?? 0
    long_description = value["long_description"] as? String ?? ""
    place_id = value["place_id"] as? String ?? ""
    short_description = value["short_description"] as? String ?? ""
    stamp_price = value["stamp_price"] as? Int ?? 0
    stamps = value["stamps"] as? Int ?? 0
    start_date = value["start_date"] as? Int ?? 0
    title = value["title"] as? String ?? ""
    key = snapshot.key
}

现在我可以使用以下内容将快照放入结构中:

let promotion = PromotionsDataModelFirebase(snapshot: snapshot)