Swfit:Firebase DB / Dictionary寻找最高的Int

时间:2017-03-30 11:23:51

标签: swift firebase firebase-realtime-database nsdictionary

我有一个Firebase数据库,看起来像图像enter image description here

当我通过NSDictionary在我的代码中获取这些值时,我想检查什么是最高整数,所以在这种情况下为4。

这就是NSdictionary的外观(我不能像当我有一个数组来得到这样的字典名称[1]时...):

version: "2.0"
services:
  node:
    build:
      context: .
  args:
    http_proxy: ${http_proxy}
    https_proxy: ${http_proxy}
  links:
    - mongo
  ports:
    - "80:80"
    - "443:443"
    - "8080"
    - "9000:9000"
  mongo:
    image: mongo:3.2
  ports:
    - "27017"

Firebase或词典中是否有任何选项可以帮助我实现此功能? 我已经找到了一些解决方案,但所有这些都不是我想要的。也许有人知道怎么做?

我很高兴知道

编辑1: 问题是我从firebase得到了我的字典:

  {
   1 =     {
      driverName = chauffeur1;
      latitude = "37.33022769";
      longitude = "-122.03439793";
  };
  4 =     {
      driverName = kees;
  };
}

我不能这样说:

DBProvider.Instance.dbRef.child("driverLocations").observeSingleEvent(of: .value, with: { (snapshot) in
        if let data = snapshot.value as? NSDictionary {
            print(data)
            let keys = data.allKeys
            let sortedKeys = Array(keys).sorted(by: { $0 > $1 }) // highest will be the first
            let highestKey = sortedKeys[0]
        }
    })

也许我只是有点愚蠢做错了..你知道我怎么解决它吗?

编辑2(snapshot.value数据)

 if let data = snapshot.value as? NSDictionary[Int: Any]

3 个答案:

答案 0 :(得分:1)

尝试这样的事情:

https://r8---sn-gxap5ojx-cage.googlevideo.com/videoplayback?upn=P6gEaXLtqOc&ei=ROrcWMzGKsjfoAPtl6XYAg&id=o-AIBZRxwo9AvJU4q02_xR0lfxJbSV3pxpga67Si4bLieu&key=yt6&ip=139.59.64.12&ipbits=0&pl=20&requiressl=yes&initcwndbps=8882500&ratebypass=yes&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&itag=18&expire=1490894500&mime=video%2Fmp4&pcm2cms=yes&source=youtube&mv=m&mt=1490872775&ms=au&lmt=1487309879666192&clen=8449602&gir=yes&mn=sn-gxap5ojx-cage&mm=31&dur=221.518&signature=6ED71F39CAA3B6719A8BAAEABD51D568ABF4D7A3.B3EED034140574BAC9733EC5CC28EF4AC9E3F758&title=Kygo Selena Gomez It Ain t Me with Selena Gomez Audio

希望有所帮助

答案 1 :(得分:1)

你需要这样的东西。

if let dictionary = snapshot.value as? [Int:[String:Any]] {
    if let max = Array(dictionary.keys).max() {
        print(dictionary[max])
    }
}

如果您的词典keyString,请使用数字尝试。

if let dictionary = snapshot.value as? [String:[String:Any]] {
    if let max = Array(dictionary.keys).flatMap({ Int($0) }).max() {
        print(dictionary["\(max)"])
    }
}

答案 2 :(得分:1)

let dict = ["1": ["driverName": "chauffeur1",
                "latitude": 37.33022769,
                "longitude": -122.03439793],
            "4": ["driverName": "kees"]]

if let maxElememt = dict.max(by: { $0.key<$1.key }) {
    maxElememt.key    // "4"
    maxElememt.value    // ["driverName": "kees"]
}

let maxValue = dict.max{$0.key<$1.key}.map{$0.value}
maxValue   // ["driverName": "kees"]

如果您需要它,只需按键减少其值

let sortedDic = dict.sorted{ $0.key>$1.key }

sortedDic.first          //  (key "4", ["driverName": "kees"])
sortedDic.first?.key     //  "4"
sortedDic.first?.value   // ["driverName": "kees"]
sortedDic.first?.value["driverName"] // "kees"