Kaz
Kaz

Reputation: 3

"use of unresolved identifier" SWIFT

The error

use of unresolved identifier 'slider'

comes where it says

var sliderValue=slider.value

What's the problem??

 import UIKit

    class ViewController: UIViewController {

        @IBAction func colorChanger(sender: UISlider) {
            // creates variable to hold new color
            var newBackgroundColor : UIColor

            // creates variable holding the value from slider
            var sliderValue = slider.value

            // changes the newBackgroundColor variable to new color values.
            newBackgroundColor = UIColor(hue: sliderValue, saturation: 0.5, brightness: 0.5, alpha: 0.5)

            // changes the background color
            self.view.backgroundColor = newBackgroundColor    }

Upvotes: 0

Views: 1929

Answers (2)

pbodsk
pbodsk

Reputation: 6876

You dont have a slider property defined in your code as far as I (or Swift ;-)) can see.

Instead you have a parameter called sender in your method which is a UISlider so try using that instead.

var sliderValue = sender.value

or

rename sender to slider

So here's one way of doing it (with some help from @leo-dabus). I took the liberty of cleaning up you code a little too, sorry :-)

import UIKit

class ViewController: UIViewController {

@IBAction func colorChanger(sender: UISlider) {
    // creates variable holding the value from slider
    let sliderValue = CGFloat(sender.value)

    // changes the newBackgroundColor variable to new color values.
    let newBackgroundColor = UIColor(hue: sliderValue, saturation: 0.5, brightness: 0.5, alpha: 0.5)

    // changes the background color
    view.backgroundColor = newBackgroundColor    
}

Upvotes: 1

user3182143
user3182143

Reputation: 9609

I got the solution for your question

First hook up the slider

@IBOutlet var slider: UISlider!

Then set minimum,maximum and current value whatever you want in storyboard or xib or programmatically

let sliderValue = Int(slider.value)
print("Slider value is now - \(sliderValue)")

Output

Slider value is now - 6

Upvotes: 0

Related Questions