Flo
Flo

Reputation: 449

Cannot invoke initializer for type 'Float' with an argument list of type '(Measurement<UnitLength>)'

I'm setting up a new function and want to convert Feet and Inches to Centimeters. I'm getting the right value converted in centimeters but when I try to assign that value to an UISlider I'm getting an error.

The error is: Cannot invoke initializer for type 'Float' with an argument list of type '(Measurement<UnitLength>)'

I already tried to cast it to different types and also to assign the value to different types of new variables but I can't manage to remove the error.

Here is my code for my function:

func receiveHeightImperial(feet: Int?, inches: Int?) {

    print("\n\nImperial Data received")
    print("Feet: \(feet ?? 0)")
    print("Inches: \(inches ?? 0)")

    let feet = Measurement(value: Double(feet ?? 0), unit: UnitLength.feet)
    let inches = Measurement(value: Double(inches ?? 0), unit: UnitLength.inches)

    let feetToCm = feet.converted(to: UnitLength.centimeters)
    let inchesToCm = inches.converted(to: UnitLength.centimeters)

    print("\nFeet to cm: \(feetToCm)")
    print("Inches to cm: \(inchesToCm)")

    let sumOfCm = feetToCm + inchesToCm
    print("Sum: \(sumOfCm)")

    vehicleHeightSliderValue = Float(sumOfCm) // ERROR here

    tableView.reloadData()
}

I expect to see the UISlider set with the new value but I can't manage to pass that error. Should I do the conversion in a different way to avoid this problem or is a possibility to fix this ?

Here is what I get in the console when I run this function:

enter image description here

Thanks if you read this.

Upvotes: 0

Views: 129

Answers (2)

Joakim Danielson
Joakim Danielson

Reputation: 51973

Change sumOfCm to sumOfCm.value to get it as a Double

Upvotes: 1

David Pasztor
David Pasztor

Reputation: 54735

You need to access the value property of Measurement, which is a Double, then convert that to a Float.

vehicleHeightSliderValue = Float(sumOfCm.value)

Upvotes: 2

Related Questions