How can I restrict iOS slider value to an integer?

Keyur Padalia picture Keyur Padalia · Jan 5, 2017 · Viewed 15.7k times · Source

I am using Swift 3 and Xcode 8.2. Pretty new at Swift. I have a horizontal UISlider to control the volume from 1 to 16 as min and max value, and it's returning float values when I do print(). How can I restrict it's value to an integer?

 @IBOutlet var ringValue: UISlider!

 @IBAction func ringVolumeSliderChange(_ sender: UISlider)
 {
        print(sender.value)
 }

Thanks in advance!

Answer

Nirav D picture Nirav D · Jan 5, 2017

value property of UISlider is of Float type so you cannot change that but you can convert it to Int using Int(sender.value).

@IBAction func ringVolumeSliderChange(_ sender: UISlider) {
    print(Int(sender.value))
}