WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.