NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Cannot Conform when Documentation Says It Can

Forums > SwiftUI

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)
        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!


How are you calling it in your VStack? We need to see the part that is causing the error to accurately diagnose the problem.

My guess, though, is that you are calling it somewhere that expects a String, which is fine, but that you left off the (), which is not.


 var body: some View {
        VStack {


That is how it is being called. I'm just working on methods and computed properties right now. Once I know they all work, I plan on building out the views. The exact error is below:

Type '(ContentView) -> (Float16, Float16) -> String' cannot conform to 'LosslessStringConvertible'


Yep, just as I thought.


legTime is a function so you need to call it like this:


Otherwise, you aren't supplying the String result of the legTime function to the Text, you are trying to convert the legTime function itself into a String and feed that into the Text.


Ah! This makes a lot more sense. Thank you so much for the reply and the help.


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until October 1st.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.