我需要编一本字典,格式是[国家:代表]。我保存了一份代表名单,里面有关于这位代表的一些相关信息。看起来是这样的:
swift - 如何基于Firebase创建字典,但是定义的值是子节点,定义的值是父节点[Swift]-LMLPHP
我需要根据这些代表身上的国家价值对他们进行分类。
我试图隔离每个状态值,但它最终只打印出整个子快照。

ref = Database.database().reference(fromURL: "Redacted for Privacy")
    ref.observeSingleEvent(of: .value, with: { snapshot in
        print(snapshot.childrenCount) // I got the expected number of items
        let enumerator = snapshot.children
        while let rest = enumerator.nextObject() as? DataSnapshot {
            print(rest.value!)
        }
    })

我怎么可能存储每个代表,使他们实际上是由他们的状态定义的?例如,这本词典中的一个条目可能是阿拉巴马州(Jo Bonner、Martha Roby、Mike Rogers、Robert B Aderholt、Mo Brooks、Spencer Bachus、Terri A Sewell)。我所发现的例子似乎都无法改变。
我试过使用这个代码:
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(6), execute: {
        print("six second delay")
        print("entered function")
        var i: Int = 0
        var x: Int = 0
        while(i < self.repList.count){
            print("i = " + String(i))
            self.ref = Database.database().reference(fromURL: "Redacted for privacy" + "/" + self.repList[i] + "/" + "State")
            self.ref.observeSingleEvent(of: .value, with: { snapshot in
                print(snapshot.value!)
                let valString = snapshot.value! as? String
                if (valString != nil){
                    self.captured = String(valString!)
                    print("Noice")

                }
                else {
                    print("ya done f***** up")
                }
            })
            while(x < self.stateCongmanDict.count){
                print("x = " + String(x))
                if (self.stateCongmanDict[self.captured] != nil){
                    self.alreadyThere = true
                    break
                }
                x = x + 1
            }
            self.stateCongmanDict[self.captured] = [self.captured]
            i = i + 1
        }
        i = 0
        x = 0
        while (i < self.repList.count){
            print("i restarted = " + String(i))
            self.ref = Database.database().reference(fromURL: "Redacted for privacy" + "/" + self.repList[i] + "/" + "State")
            self.ref.observeSingleEvent(of: .value, with: { snapshot in
                print(snapshot.value!)
                let valString = snapshot.value! as? String
                if (valString != nil){
                    self.captured = String(valString!)
                    print("Noice")

                }
                else {
                    print("ya done f***** up")
                }
            })
            while(x < self.stateCongmanDict.count){
                print("restarted x = " + String(x))
                if (self.stateCongmanDict[self.captured]! != [""]){
                    self.alreadyThere = true
                    break
                }
                else {
                    var c: Int = 0
                    var capturedDos: String = ""
                    while (c < self.repList.count - 1){
                        print("c = " + String(c))
                        self.ref = Database.database().reference(fromURL: "Redacted for privacy" + "/" + self.repList[c] + "/" + "State")
                        self.ref.observeSingleEvent(of: .value, with: { snapshot in
                            print(snapshot.value!)
                            let valString = snapshot.value! as? String
                            if (valString != nil){
                                capturedDos = String(valString!)
                                print("Noice")
                            }
                            else {
                                print("ya done f***** up")
                            }
                        })
                        if (capturedDos == self.captured){
                            print("DO YOU SEE ME!!!!")
                            self.stateCongmanDict[capturedDos]?.append(self.repList[i])
                        }
                        c = c + 1
                    }
                }
                x = x + 1
            }
            i = i + 1
        }
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(6), execute: {
            print("six second delay check state")
            print("succeed if seen:" + String(describing: self.stateCongmanDict))
        })
    })

不幸的是它只是打印了一个空的口述:
六秒延迟检查状态
如果看到成功:
["": ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]]

最佳答案

我认为你需要反复检查这些孩子,然后检查字典中是否已经存在状态,所以我认为你可以做这样的事情(我没有测试过,但是想法是一样的):

var dictionary = [String: [String]]()
ref.observeSingleEvent(of: .value, with: { snapshot in
        print(snapshot.childrenCount) // I got the expected number of items
        let enumerator = snapshot.children
        while let rest = enumerator.nextObject() as? DataSnapshot {
            print(rest.value!)
            let name = rest.key
            if let value = rest.value as? [String: Any] {
                  let state = value["State"] as? String
                  if var repsArr = self.dictionary[state] as? [String] {
                          repsArr.append(name)
                          self.dictionary[state] = repsArr
                  }else {
                          let newRepsArr = [name]
                          self.dictionary[state] = newRepsArr
                  }
                  print("Dictionary Inside while loop: \(self.dictionary)") //watch dictionary build itself
            }

        }
        print("Dictionary: \(self.dictionary)")
    })

关于swift - 如何基于Firebase创建字典,但是定义的值是子节点,定义的值是父节点[Swift],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47046608/

10-10 21:48