如何从 swift 3 中包含双精度和整数的一组值中获取最大值和最小值?

How to get max and min from set of values which contain double and integer in swift 3?

这里我需要从包含双精度和整数的一组数字中获取最大值和最小值,在这里我尝试了下面的代码但它显示错误并且错误是 Cannot invoke 'max' with an argument list of type '([Any?])' 谁能帮助我如何解决这个问题?

这是我的代码

   var facetsModel = [ListFacets]()

   for (key, value) in (dict as? [String:Any])! {
      print(key)
      print(value)
      var dict = [String:Any]()
      dict.updateValue(key , forKey: "price")
      dict.updateValue(value , forKey: "quantity")
      self.facetsModel.append(ListFacets.init(dict: dict))
    }
    let maxNum = max(self.facetsModel.map{[=11=].key})
    print(maxNum)

这是我的模型class

struct ListFacets {

    let key : Any?
    let value : Int?

    init(dict:[String:Any]) {
        if let price = dict["price"] as? Double {
            self.key = price
        }else {
           self.key =  dict["price"]
        }
        self.value = dict["quantity"] as? Int
    }

}

此处显示字典数据

{ 0 = 1; "2.1" = 2; 21 = 3; "31.5" = 2; "9.45" = 1; }

尚不完全清楚您要做什么,但这是设置结构和查找最高价格的更简洁的方法:

struct ListFacets {

    let quantity : Int
    let price : Double
}

var facetsModel = [ListFacets]()

let dict = ["0": 1, "2.1":2, "21":3, "31.5": 2, "9.45":1]

for (priceStr, quantity) in dict {
    if let price = Double(priceStr) {
        facetsModel.append(ListFacets(quantity: quantity, price: price))
    }
}


if let maxNum = facetsModel.max( by: { (a, b) -> Bool in
    return a.price < b.price
    }) {
        print(maxNum)
}