UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

1      

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.

1      

 var body: some View {
        VStack {
            Text(String(fuelInPounds))
            Text(String(legTime))

        }
        .padding()
    }

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'

1      

Yep, just as I thought.

Text(String(legTime))

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

Text(legTime())

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.

2      

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

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.