将DataSnapshot转换为自定义结构对象-Swift 4

时间:2018-10-27 22:19:26

标签: firebase firebase-realtime-database swift4

因此,在我的项目中,我创建了一个自定义结构:

import Foundation
import Firebase

struct CheckedIn {

    let ref: DatabaseReference?
    let key: String
    let max_cm: String
    let max_in: String
    let weight_kg: Int
    let weight_lb: Int

    init(key: String = "", max_cm: String, max_in: String, weight_kg: Int, weight_lb: Int) {

        self.ref = nil
        self.key = key
        self.max_cm = max_cm
        self.max_in = max_in
        self.weight_kg = weight_kg
        self.weight_lb = weight_lb

    }

    init?(snapshot: DataSnapshot) {
        guard
            let value = snapshot.value as? [String: AnyObject],
            let max_cm = value["checked_max_cm"] as? String,
            let max_in = value["checked_max_in"] as? String,
            let weight_kg = value["checked_weight_kg"] as? Int,
            let weight_lb = value["checked_weight_in"] as? Int
        else {
            return nil
        }

        self.ref = snapshot.ref as DatabaseReference
        self.key = snapshot.key
        self.max_cm = max_cm
        self.max_in = max_in
        self.weight_kg = weight_kg
        self.weight_lb = weight_lb

    }

    func toAnyObject() -> Any {

        return [

            "checked_max_cm": self.max_cm,
            "checked_max_in": self.max_in,
            "checked_weight_kg": self.weight_kg,
            "checked_weight_in": self.weight_lb

        ]
    }
}

这是我的数据库的结构:

我正在尝试使用以下代码在视图控制器中检索一些数据:

ref.queryOrderedByKey().observe(.value) { (snapshot) in

    var newItems: [CheckedIn] = []

    for child in snapshot.children {

        print(child)

        if let snapshot = child as? DataSnapshot, let company = CheckedIn(snapshot: snapshot) {

            newItems.append(company)

        }

    }

    print(newItems)
    self.companiesCheckedIn = newItems
    self.tableView.reloadData()
    UIViewController.removeSpinner(spinner: sv)

}

问题在于,每当我尝试打开视图控制器时(此代码段位于.viewDidLoad内部),列表“ newItems”为空。奇怪的是,当我尝试跑步时

  

打印(子)

所有数据都已打印

所以我认为'if let snapshot = child as? DataSnapshot,让company = CarryOn(snapshot:snapshot)'块未运行,该怎么办?

2 个答案:

答案 0 :(得分:0)

设法解决此问题:

此代码位于ViewController的viewDidLoad()方法之前

let ref = Database.database().reference().child("alldata")
var companyName: [String] = []
var maxCm: [String] = []
var maxIn: [String] = []
var weightKg: [Int] = []
var weightLb: [Int] = []

viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()

    ref.queryOrderedByKey().observeSingleEvent(of: .value) { (snapshot) in

        let snapshotValue = snapshot.value as! [String:[String:AnyObject]]
        let sorted = snapshotValue.sorted() { $0.key.lowercased() < $1.key.lowercased() }

        for (key, _) in sorted {

            let keys = snapshotValue[key]
            let company_name = key
            self.companyName.append(company_name)
            let carry_max_cm = keys?["keyname"] as! String
            self.maxCm.append(carry_max_cm)
            let carry_max_in = keys?["keyname"] as! String
            self.maxIn.append(carry_max_in)
            let carry_weight_kg = keys?["keyname"] as! Int
            self.weightKg.append(carry_weight_kg)
            let carry_weight_lb = keys?["keyname"] as! Int
            self.weightLb.append(carry_weight_lb)

        }

        self.tableView.reloadData()

    }
}

let sorted = snapshotValue.sorted() { $0.key.lowercased() < $1.key.lowercased() }

按字母顺序对字典

答案 1 :(得分:-1)

“ checked_weight_kg”和“ checked_weight_in”是整数类型,但您分配了字符串类型。

您的代码->

let weight_kg = value["checked_weight_kg"] as? String,
let weight_lb = value["checked_weight_in"] as? String 

更改->

init?(snapshot: DataSnapshot) {
        guard
            let value = snapshot.value as? [String: AnyObject],
            let max_cm = value["carry_max_cm"] as? String,
            let max_in = value["carry_max_in"] as? String,
            let weight_kg = value["checked_weight_kg"] as? Int,
            let weight_lb = value["checked_weight_in"] as? Int
            else {
                return nil
        }

        self.ref = snapshot.ref as DatabaseReference
        self.key = snapshot.key
        self.max_cm = max_cm
        self.max_in = max_in
        self.weight_kg = weight_kg
        self.weight_lb = weight_lb

    }