Neulio
Neulio

Reputation: 332

UI Slider display an array of Strings

I have a UISlider and an array of strings to display in a UILabel. When trying to set the minimumValue & maximumValue I get this error Cannot assign value of type 'String' to type 'Float'

I am new to swift and I have made a slider for Ints but never Strings.

import UIKit


class PostTableViewController: UITableViewController, UINavigationControllerDelegate {

    @IBOutlet weak var ageSlider: UISlider!
    @IBOutlet weak var ageSliderLabel: UILabel!

     var ageArray = ["6 weeks", "7 weeks", "8 weeks", "9 weeks", "10 weeks", "11 weeks", "12 weeks", "13 weeks", "14 weeks", "15 weeks", "16 weeks", "17 weeks", "18 weeks", "19 weeks", "20 weeks", "21 weeks","22 weeks","23 weeks","24 weeks","25 weeks","26 weeks","27 weeks","28 weeks","29 weeks","30 weeks",]





        override func viewDidLoad() {
            super.viewDidLoad()
            let tapGesture = UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing))
            view.addGestureRecognizer(tapGesture)


        }


        // MARK:Table view data source
        override func numberOfSections(in tableView: UITableView) -> Int {
            return 6
        }

        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 1
        }
        func ageSliderInterval(){
            ageSlider.minimumValue = String("")// This is where i get the error
            ageSlider.maximumValue = String(ageArray.count)// This is where i get the error
            ageSlider.isContinuous = false
        }
        @IBAction func ageSlider(_ sender: Any) {

        }
        }

Upvotes: 2

Views: 252

Answers (2)

Kishan Bhatiya
Kishan Bhatiya

Reputation: 2368

You can try with this,

And slider's max and min value is type of float not string

ageSlider.minimumValue = 0 
ageSlider.maximumValue = ageArray.count

And

@IBAction func ageSlider(_ sender: Any) {

    let currentValue = Int(sender.value)
    ageSliderLabel.text = ageArray[currentValue]
}

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

You can try

ageSlider.minimumValue = 0  // check even if it's the defailt 
ageSlider.maximumValue =  geArray.count 

and change this function name

@IBAction func ageSlider(_ sender: Any) {

as it confuses with variable name

Upvotes: 0

Related Questions