Hey All,
I'm working with a function that will calculate the time a given leg of a trip will take to complete. I've gotten several errors along the way and I'm very confused with what is happening...
This is the function:
func legTime (speed: Float16, distance: Float16) -> String {
let time = legDistance / groundspeed
let hours = Int(time)
let minutes = Int((time * 60).truncatingRemainder(dividingBy: 60))
let seconds = Int((time * 3600).truncatingRemainder(dividingBy: 60))
let formattedTime = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
print(formattedTime)
return formattedTime
The variables are as follows:
@State private var groundspeed :Float16 = 100
@State private var legDistance :Float16 = 100
Originally, the initializer was composed as follows:
unc legTime (speed: Int, distance: Int) -> String
This gave me errors on the .truncatingRemainder call as "Binary operator '' cannot be applied to operands of type 'Int' and 'Float16'." I assumed this meant that in some instances the float value would be large enough to require a float16, so I changed the initializer, and declared groundspeed
and legDistance
as float16's (as referenced above). The problem with THAT, though, is I'm now getting the following error when I try to call the function in my VStack: Type '(ContentView) -> (Float16, Float16) -> String' cannot conform to 'StringProtocol'
. Float16 is a conforming type in String, per Apple's documentation, so I'm not sure how to read this error.
Any help would be appreciated. Thanks!