Search code examples
iosswiftuislider

How do you round a UISlider to an int in swift


I was wondering how to round a UISilder value to a int value. Right now it is displaying floats. I am using swift 3 at the moment.

     @IBOutlet var slidermove: UISlider!

     @IBAction func SliderVal(_ sender: AnyObject) {

         print(slidermove)
         let x = round(slidermove)
  }

round() does not work it will give an error, thanks for anyone that helps


Solution

  • You should round it's value instead of UISlider itself.:

    let x = roundf(slider.value) // x is Float
    

    If you need an Int instead of Float, just wrap it with Int initializer:

    let x = Int(round(slider.value)) // x is Int