0

I have a UISlider with value from 0 to 8 (meters). I try to convert from Meters to CM, Feet, Inches and to display this values on 2 UILabels. In one label I want to show the value like: 19 ft 10" and in another label I want to show the value like: 6 m 4 cm. I'm struggling with the conversion and maybe someone can help me display this values.

Here is my code in the View:

protocol VehicleHeightCellDelegate {
    func vehicleHeightSliderValueChanged(_ slider: UISlider, _ feetsLabel: UILabel, _ metersLabel: UILabel)
}

class VehicleHeightCell: UITableViewCell {

    // Interface Links
    @IBOutlet weak var vehicleHeightSlider: UISlider!
    @IBOutlet weak var vehicleHeightFeetsLabel: UILabel!
    @IBOutlet weak var vehicleHeightMetersLabel: UILabel!

    // Properties
    var delegate: VehicleHeightCellDelegate?

@IBAction func vehicleHeightValueChanged(_ sender: UISlider) {

    delegate?.vehicleHeightSliderValueChanged(vehicleHeightSlider, vehicleHeightFeetsLabel, vehicleHeightMetersLabel)
    }
}

Here is my code in the Controller:

extension ChecklistInspectionController: VehicleHeightCellDelegate{

    func vehicleHeightSliderValueChanged(_ slider: UISlider, _ feetsLabel: UILabel, _ metersLabel: UILabel) {

        let currentValue = Int(slider.value)

        let heightMeters = Measurement(value: Double(currentValue), unit: UnitLength.meters)
        let heightFeet = heightMeters.converted(to: UnitLength.feet)

        let heightCentimeters = heightMeters.converted(to: UnitLength.centimeters)
        let heightInches = heightMeters.converted(to: UnitLength.inches)


        feetsLabel.text = "\(heightFeet)" + " \(heightInches)"
        metersLabel.text = "\(heightMeters)" + " \(heightCentimeters)"
    }
}

The result needs to look like this:

enter image description here

At the moment look like this: enter image description here

Thanks if you read this !

Florentin Lupascu
  • 754
  • 11
  • 30

4 Answers4

3

Here's a snippet that converts feet to meters:

return Measurement(value: ft, unit: UnitLength.feet).converted(to: .meters).value

As the answer above suggested, see the documentation for all your options

Ron
  • 660
  • 1
  • 5
  • 10
2

If you want to have the values displayed separately, you have to round your results and extract the decimal part and convert it.

Here is how you can achieve your results:

 //EXAMPLE
 let heightMeters = Measurement(value: 16.99, unit: UnitLength.meters)

 let heightCentimeters = heightMeters.converted(to: UnitLength.centimeters)

 //EXTRACT CM FROM METERS BY EVALUATING THE DECIMAL PART
 let cm = ceil((heightMeters.value - floor(heightMeters.value))*100)

 // GET METERS WITHOUT CM
 let meters = floor(heightMeters.value)

 let heightInches = heightMeters.converted(to: UnitLength.inches)
 let heightFeet = heightMeters.converted(to: UnitLength.feet)

 //EXTRACT INCHES FROM FT BY EVALUATING THE DECIMAL PART
 let inches = ceil((heightFeet.value - floor(heightFeet.value))*12)

 // GET FT WITHOUT INCHES
 let feet = floor(heightFeet.value)

EDIT:

You can use round instead of ceil to automatic round to the nearest int number or you can round only the decimal part (or truncate an arbitrary amount of numbers of decimal part) in order to have decimal numbers like "6 m and 7.12 cm" or "12 ft and 11.4 inches"

Andrew21111
  • 868
  • 8
  • 17
0

You'll want to use a MeasurementFormatter. You can use it's numberFormatter property to configure how you want to display the number part, that way you can remove the decimals.

If you set unitOptions to .naturalScale you'll probably get the style you want.

Here's a link to the class reference.

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
0

I did it in this way to keep things simple:

extension ChecklistInspectionController: VehicleHeightCellDelegate{

func vehicleHeightSliderValueChanged(_ slider: UISlider, _ feetsLabel: UILabel, _ metersLabel: UILabel) {

    let currentValue = Int(slider.value)
    let meters = currentValue / 100
    let centimeters = currentValue % 100
    let inches = currentValue < 3 ? 0 : round(Double(currentValue) / 2.54)
    let feet = round(inches / 12)
    let inch = round(inches.truncatingRemainder(dividingBy: 12))

    feetsLabel.text = "\(feet) ft" + " \(inch)\""
    metersLabel.text = "\(meters) m" + " \(centimeters) cm"

    print("\(meters) m \(centimeters) cm")
    print("\(feet) ft \(inch) \"") 
}

}

The result:

enter image description here

Florentin Lupascu
  • 754
  • 11
  • 30