0

我有一个问题,我很高兴能得到帮助,我向 API 服务器发送了一个发布请求,我成功地得到了响应,并使用“array.last”过滤掉了数组中的最后一个元素结果如下所示,我惊讶地发现最后一个元素本身又看起来像一个数组,我想进一步过滤它并根据概率最高的元素获得结果。例如,在我的数组的最后一个元素中,我有以下两个。

ClarifaiModel(名称:“女性”,概率:0.8970829) ClarifaiModel(名称:“男性”,概率:0.10291709)

概率最高的是 ClarifaiModel(名称:“女性”,概率:0.8970829)。我希望这是有道理的,我总是可以回答更多问题。谢谢。

我的元素数组中的最后一个元素如下,

ClarifaiModel(name: "39", probability: 0.6835152)
ClarifaiModel(name: "38", probability: 0.6712693)
ClarifaiModel(name: "37", probability: 0.65020597)
ClarifaiModel(name: "36", probability: 0.6027025)
ClarifaiModel(name: "40", probability: 0.5968272)
ClarifaiModel(name: "41", probability: 0.50408536)
ClarifaiModel(name: "35", probability: 0.46325612)
ClarifaiModel(name: "42", probability: 0.39715457)
ClarifaiModel(name: "34", probability: 0.3904736)
ClarifaiModel(name: "33", probability: 0.29972908)
ClarifaiModel(name: "43", probability: 0.2892449)
ClarifaiModel(name: "44", probability: 0.2792564)
ClarifaiModel(name: "45", probability: 0.17198724)
ClarifaiModel(name: "32", probability: 0.16969433)
ClarifaiModel(name: "31", probability: 0.16112417)
ClarifaiModel(name: "30", probability: 0.14761531)
ClarifaiModel(name: "29", probability: 0.13462797)
ClarifaiModel(name: "28", probability: 0.119995646)
ClarifaiModel(name: "46", probability: 0.108111665)
ClarifaiModel(name: "27", probability: 0.103014976)
ClarifaiModel(name: "feminine", probability: 0.8970829)
ClarifaiModel(name: "masculine", probability: 0.10291709)
ClarifaiModel(name: "asian", probability: 0.9898949)
ClarifaiModel(name: "native hawaiian or pacific islander", probability: 0.011381255)
ClarifaiModel(name: "hispanic, latino, or spanish origin", probability: 0.0006249249)
ClarifaiModel(name: "white", probability: 0.00053802715)
ClarifaiModel(name: "american indian or alaska native", probability: 0.0003932913)
ClarifaiModel(name: "black or african american", probability: 0.00021348728)
ClarifaiModel(name: "middle eastern or north african", probability: 3.989372e-05)

我的 ClarifaiModel 类如下所示,

import Foundation

struct ClarifaiModel {
    let name: String
    let probability: Double
}


extension ClarifaiModel: Comparable {
    static func < (lhs: ClarifaiModel, rhs: ClarifaiModel) -> Bool {
        lhs.probability < rhs.probability
    }
}

我的视图控制器类的一部分如下所示,我从 API 获取数据并尝试从数组中过滤掉我需要的内容。

 guard let data = data else { return }

                  // And finally, we are going to parse the JSON data and cast it as a dictionary so we can pull out the fact text to display it.
                  do {
                      guard let clarifaiData = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any] else { return }
                      guard let outputs = clarifaiData["outputs"] as? [[String: Any]] else { return }
                      guard let outputsData = outputs[0]["data"] as? [String: Any] else { return }
                      guard let regions = outputsData["regions"] as? [[String: Any]] else { return }
                      guard let regionsData = regions[0]["data"] as? [String: Any] else { return }
                      guard let concepts = regionsData["concepts"] as? [[String: Any]] else { return }
                    //  print(concepts)



                      for concept in concepts {

                          let name = concept["name"] as? String ?? ""
                          let certainty = concept["value"] as? Double ?? 0.0000

                          let newModel = ClarifaiModel(name: name, probability: certainty)
                        self.clarifaiModel.append(newModel)
                    //  let a =  self.clarifaiModel.last!
                        let array = [
                            self.clarifaiModel
                        ]
                        let max = array.max()
                        print(max!)


                      }

我在“Sequence”上收到“引用实例方法“max()”的错误,要求“[ClarifaiModel]”符合“Comparable”我以前从未使用过这种可比较的协议,根本不理解它。请帮忙

4

2 回答 2

0
yourArray.sort(by: { $0.probability > $1.probability })
print(yourArray.first)
于 2020-06-09T07:07:20.000 回答
0
extension ClarifaiModel: Comparable {
    static func < (lhs: ClarifaiModel, rhs: ClarifaiModel) -> Bool {
        lhs.probability < rhs.probability
    }
}

let array = [
    ClarifaiModel(name: "39", probability: 0.6835152),
    ClarifaiModel(name: "38", probability: 0.6712693),
    ClarifaiModel(name: "37", probability: 0.65020597),
    ClarifaiModel(name: "36", probability: 0.6027025)
]
let max = array.max()

如果您有评论中提到的数组数组:

let arrays = [array, array]
let maxx = arrays.flatMap({$0}).max()
于 2020-06-08T21:27:56.883 回答