Nikolay
Nikolay

Reputation: 77

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 :-(

Upvotes: 0

Views: 177

Answers (2)

tommybananas
tommybananas

Reputation: 5736

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.

Upvotes: 2

Rob Napier
Rob Napier

Reputation: 299345

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

Upvotes: 2

Related Questions