简体   繁体   中英

Swift iOS convert ArraySlice to CGFloat

I have an array with Int values. In this array I need to get the highest value. Until here I have no problem. But now I need to convert this value to a 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)

How can I convert ArraySlice to CGFloat ? All I have tried failed :-(

let maxOrdersPerHourVal = ordersPerHour.sort.last!

or

let maxOrdersPerHourVal = ordersPerHour.max()

will get the max value in an array.

Then you can cast as normal var floatVal = CGFloat(maxOrdersPerHourVal) if you need to cast the value.

Don't use sort here. Just use max() (or maxElement() in older versions of Swift). That will return an Int rather than a slice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM