我有一个带有Int值的数组。在这个数组中,我需要获得最高价值。直到这里我没有问题。但现在我需要将此值转换为CGFloat。
let ordersPerHour = [hour0All, hour1All, hour2All, hour3All, hour4All, hour5All, hour6All, hour7All, hour8All, hour9All, hour10All, hour11All, hour12All, hour13All, hour14All, hour15All, hour16All, hour17All, hour18All, hour19All, hour20All, hour21All, hour22All, hour23All]
let maxOrdersPerHourVal = ordersPerHour.sort().suffix(1)
如何将ArraySlice
转换为CGFloat
?我尝试过的都失败了: - (
答案 0 :(得分:2)
let maxOrdersPerHourVal = ordersPerHour.sort.last!
或
let maxOrdersPerHourVal = ordersPerHour.max()
将获得数组中的最大值。
如果您需要转换值,则可以正常投射var floatVal = CGFloat(maxOrdersPerHourVal)
。
答案 1 :(得分:2)
请勿在此处使用sort
。只需在旧版本的Swift中使用max()
(或maxElement()
)。那将返回一个Int而不是一个切片。