gpt4 book ai didi

arrays - Swift3 iOS -Firebase 如何将节点数据分离到不同的数组和字典中

转载 作者:可可西里 更新时间:2023-11-01 02:02:43 26 4
gpt4 key购买 nike

root
|
@-clothing
|
@-nameBrandRalphLauren//topLevel
| |
| @-apparelTops//child
| | |-"garmetSweater":"white"
| | |-"garmetTshirt":"blue"
| | |-"postedBy":"user123"
| |
| @-apparelBottoms//child
| |-"garmetShorts":"tan"
| |-"garmetJeans":"blue"
| |-"postedBy":"user123"
|
@-nameBrandNike//topLevel
|
@-apparelTops//child
|-"garmetSweater":"gray"
|-"garmetTshirt":"pink"
|-"postedBy":"user789"

我在 Firebase 中有几个顶级节点,它们下面还有子节点。子节点内部是键和值。我想将每个 topLevelNode (RalphLauren,Nike) 分成包含其子节点 (apparelTops, apparelBottoms) 的数组,然后将它们放入自己的数组中。

我怎样才能实现这样的目标

var nameBrandNodes = [String]()
var apparelNodes = [[String]]()
var garmetNodes = [[String:Any]]()

nameBrandNodes = [RaplhLaurn, Nike]
apparelNodes = [[apparelTops, apparelBottoms], [apparelTops, apparelBottoms]]
garmetNodes = [["postedBy":"user123"], ["garmetSweater":"white"],["garmetTshirt":"blue"],["garmetShorts":"tan"], ["garmetJeans":"blue"], ["postedBy":"user789"], ["garmetSweater":"gray"], ["garmetTshirt":"pink"]]

//**EDIT** Jay's answer is CORRECT. I forgot to add this and added it after he answered
var nameBrandArrayOfDict = [[String:[String]]]

nameBrandArrayOfDict = [[nameBrandRalphLauren:[apparelTops,apparelBottoms], [[nameBrandNike:[apparelTops,apparelBottoms]]

//this is only necessary because sometimes there won't be an apparelBottomsNode or apparelTopsNode for e.g.
nameBrandArrayOfDict = [[nameBrandRalphLauren:[apparelTops], [[nameBrandNike:[apparelBottoms]]

代码如下:

let rootRef = Database.database().reference()

var nameBrandNodes = [String]()
var apparelNodes = [[String]]()
var garmetNodes = [[String:Any]]()


let clothingRef = rootRef.child("clothing")

clothingRef?.observe(.childAdded, with: {
(snapshot) in

//nameBrandNodes now contains RalphLauren and Nike
self.nameBrandNodes.append(snapshot.key)

for nameBrand in self.nameBrandNodes{

let apparel = rootRef.child("clothing").child("nameBrand")
apparel.observeSingleEvent(of: .value, with: {
(snapshot) in

for child in snapshot.children {
//what should I do in here?
}
}

我的最终结果是我使用了一个垂直的 tableView 和一个水平的 collectionsView。我将在 tableView 中列出 nameBrands,在 collectionView 中列出服装,在 collectionViews 单元格中列出服装项目。我知道如何将所有内容分开后放入 tableView 和 collectionView 中,我遇到的问题是将所有内容分成不同的数组。

最佳答案

我将抛​​出这个答案,因为它解决了这个问题,并可能提供一些方向和其他选项。

在此代码中,我们将填充一个 ClothingClass,其中包含与 Firebase 中的数据相匹配的数据。我们还有两个数组 bottomsArray 和 topsArray,它们将这些项目保存在单独的数组中(根据问题)。

这里的概念是 Firebase 中的一切都是快照,快照可以包含其他快照(即字典可以包含其他字典)。

我们通过 .value 读取整个节点,每个高级节点(名牌)是这些节点的键(键:值对中的键)。另一方面,该值也是一个快照,即

let apparelBottomsSnap = nameBrandSnap.childSnapshot(forPath: "apparelBottoms")

一旦我们有了这些快照,就将它们的值分配给字典,然后从那里添加 ClothingClass 以及单独的数组。 ClothingClass 和数组之间没有任何关联,除了使用该类是在此示例的代码中组织数据的另一种方式。

    class ClothingClass {
var nameBrand = ""
var bottomsDict = [String: Any]()
var topsDict = [String: Any]()
}

var bottomsArray = [[String: Any]]()
var topsArray = [[String: Any]]()
var clothingArray = [ClothingClass]()

let clothingRef = self.ref.child("clothing")
clothingRef.observeSingleEvent(of: .value, with: { snapshot in

for child in snapshot.children {
let nameBrandSnap = child as! DataSnapshot //this is each name_brand
let apparelBottomsSnap = nameBrandSnap.childSnapshot(forPath: "apparelBottoms")
let apparelTopsSnap = nameBrandSnap.childSnapshot(forPath: "apparelTops")

let bottomsDict = apparelBottomsSnap.value as! [String: Any]
let topsDict = apparelTopsSnap.value as! [String: Any]

let aClothing = ClothingClass()
aClothing.nameBrand = nameBrandSnap.key
aClothing.bottomsDict = bottomsDict
aClothing.topsDict = topsDict
clothingArray.append(aClothing)

bottomsArray.append(bottomsDict)
topsArray.append(topsDict)
}

//for testing
for c in clothingArray {
let n = c.nameBrand
let b = c.bottomsDict
let t = c.topsDict

print("n: \(n)")
print(" b: \(b)")
print(" t: \(t)")
}

print(topsArray)
print(bottomsArray)
})

end end 的测试循环遍历整个数组并打印每个服装类的属性

n: name_brand_0
b: ["garmetJeans": Blue, "garmetShorts": Pink, "postedBy": user_2]
t: ["garmetShirt": blue, "garmetSweater": blue, "postedBy": user_0]
n: name_brand_1
b: ["garmetJeans": Orange, "garmetShorts": Navy, "postedBy": user_3]
t: ["garmetShirt": Green, "garmetSweater": Yellow, "postedBy": user_1]

下面的打印语句打印顶部

[
["garmetShirt": blue, "garmetSweater": blue, "postedBy": user_0],
["garmetShirt": Green, "garmetSweater": Yellow, "postedBy": user_1]
]

和底部

[
["garmetJeans": Blue, "garmetShorts": Pink, "postedBy": user_2],
["garmetJeans": Orange, "garmetShorts": Navy, "postedBy": user_3]
]

关于arrays - Swift3 iOS -Firebase 如何将节点数据分离到不同的数组和字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45172718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com