如何使用FIrebase将包含对象的字典附加到数组

问题描述 投票:0回答:2

我正在使用firebase,我能够得到一个字典看起来像这样:

▿ 0 : 2 elements
    - key : "maskForHair"
    ▿ value : 3 elements
      ▿ 0 : 2 elements
        - key : id
        - value : 1
      ▿ 1 : 2 elements
        - key : name
        - value : Hair Mask
      ▿ 2 : 2 elements
        - key : note
        - value : asd
  ▿ 1 : 2 elements
    - key : "hairShampo"
    ▿ value : 3 elements
      ▿ 0 : 2 elements
        - key : id
        - value : 0
      ▿ 1 : 2 elements
        - key : name
        - value : hairShampo
      ▿ 2 : 2 elements
        - key : note
        - value : asd

现在我正在尝试使用这个字典填充tableView,但是我找不到将这个字典附加到数组的方法,任何想法?

这就是我的getCategories函数的样子:

func getCategories(){

    dbHandle = dbRefernce?.child("categories").observe(.value, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String : Any]{

            print(dictionary)

            let category = Categories(dictionary: dictionary)
            self.categoriesArr.append(category)

            DispatchQueue.main.async {
                self.categoriesTableView.reloadData()
            }
        }
    })
}

这里我的Categories类看起来如何:

import Foundation
import UIKit

class Categories: NSObject {

    var name : String? = nil
    var id : Int? = nil
    var note : String? = nil

    init (dictionary: [String: Any]) {
        super.init()
        name = dictionary["name"] as? String
        id = dictionary["id"] as? Int
        note = dictionary["note"] as? String
    }

}
ios swift firebase firebase-realtime-database
2个回答
0
投票

我首先将你的模型重命名为像HairCategory这样的单数。

您需要迭代从数据库收到的快照的子项。每个孩子应该是一个[String : Any]字典,然后可以变成HairCategory

像这样的东西:

let children = snapshot.children.compactMap { $0 as? DataSnapshot }
let cateroryDictionaries = children.compactMap { $0.value as? [String:Any] }
let categories = cateroryDictionaries.map { dict -> HairCategory in
    return HairCategory.init(dictionary: dict)
}

然后categories的类型:[HairCategory]。如果你也制作了self.categoriesArr类型的[HairCategory],你可以附加新获取的类别:)

编辑:

我还建议你看看这个库:CodableFirebase。它使得从DataSnapshot转换到你的模型和另一种方式,真的很容易..

编辑

只是为了澄清......你不能“将字典附加到数组中”,你需要将字典转换为数组,然后你可以将该数组附加到同一类型的另一个数组:)


0
投票

这就是我如何解决的问题:

 for index in 1...self.numberOfProducts{

        dbHandle = dbRefernce?.child("categories").child("\(index)").observe(.value, with: { (snapshot) in
            guard let value = snapshot.value as? [String: Any] else { return }
            guard let name = value["name"] as? String,
                let imageURL = value["imageURL"] as? String,
                let desc = value["description"] as? String,
                let price  = value["price"] as? Int,
                let id = value["subCategoryID"] as? Int
                else {return}


            let category = Product(name: name, imageURL: imageURL, description: desc, price: price,subCategoryID: id)

            self.productsArr.append(category)
            self.tblProducts.reloadData()
        })
    }

刚刚创建了一个对象,然后将其附加到数组中,您可以在循环中执行此操作

© www.soinside.com 2019 - 2024. All rights reserved.