【问题标题】:Swfit: Firebase DB / Dictionary look for highest IntSwift:Firebase DB / Dictionary 寻找最高的 Int
【发布时间】:2017-03-30 11:23:51
【问题描述】:

我有一个 Firebase 数据库,如图所示

当我通过 NSDictionary 在我的代码中获取这些值时,我想检查什么是最高整数,因此在本例中为 4。

这就是 NSdictionary 的样子(当我有一个数组来获取值时,我不能做 dictionaryName[ 1 ] 所以......):

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

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

我很高兴知道

编辑 1: 问题是我像这样从firebase获取我的字典:

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]
        }
    })

我打不出来:

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

也许我做错了有点愚蠢..你知道我该如何解决吗?

编辑 2(snapshot.value 数据)

Optional({
1 =     {
    driverName = chauffeur1;
    latitude = "37.33193453";
    longitude = "-122.03777128";
};
4 =     {
    driverName = kees;
};
})

【问题讨论】:

    标签: swift firebase firebase-realtime-database nsdictionary


    【解决方案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)"])
        }
    }
    

    【讨论】:

    • 您还应该将字典声明更改为[Int:[String:Any]]
    • @LeoDabus 我已经为 ex 添加了字典声明,但您的建议也是很好的编辑答案。
    • 是的。我认为你的答案更好。 @royvano 检查这个
    • 是的,这行得通! @VladPulichev , Int 本来是一个字符串:)
    • 好吧,我使用了@VladPulichev 的整个代码,但是从你的代码中我选择了 [string:[string:any]] 部分,我只能接受一个,所以这很困难.. 应该我愿意,我不希望你们俩都感到不被欣赏..
    【解决方案2】:

    试试这样的:

    let yourDictionary = [Int: Any]() // or [String: Any]()
    let keys = yourDictionary.keys
    let sortedKeys = Array(keys).sorted(by: { $0 > $1 }) // highest will be the first
    let highestKey = sortedKeys[0]
    

    希望对你有帮助

    【讨论】:

    • 我真的很喜欢你的回答,但是编译器说 '>' 不能应用于两个 'Any' 操作数..
    • @royvano 你的字典应该让 yourDictionary = [Int:Any]()
    • @royvano 只需将字典声明更改为 [Int:[String:Any]] 你的字典键是 Int 值是另一个字典
    • @VladPulichev 感谢您的详细回答,但我忘了提及 de dictionary 的来源。我已经更新了我的问题,您能否检查一下我做错了什么?
    • @royvano 你试过像我在你的问题评论中提到的那样吗?
    【解决方案3】:
    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"
    

    【讨论】:

      猜你喜欢
      • 2010-12-12
      • 2016-06-28
      • 1970-01-01
      • 2012-03-24
      • 2016-11-15
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多